Looking for an Automapper Database?

Is there any other way to handle a database call if I need to search for values ​​to display? It seems ugly to have database calls in display logic. How else is this handled?

For example, the table stores the SEXID, so using the resolver below I can do something like this

CreateMap<StagingPerson, Person>() .ForMember( dest => dest.SEXID, m => m.ResolveUsing<SexLookupResolver>().FromMember( src => src.Gender ) ); public class SexLookupResolver : ValueResolver<string, int> { protected override int ResolveCore( string source ) { int id = -1; if (source == "Male") id = dataAccess.GetGenderByString("M"); else if(source == "Female") id = dataAccess.GetGenderByString("F"); else if(source == "?") id = dataAccess.GetGenderByString("?"); else id = dataAccess.GetGenderByString("?"); return id; } } 

DataAccess:

 public int GetGenderByString(string gender) { string sql = "select sexid from Sexes where sex = '" + gender "'"; int sexid = ..... ...... return sexid; } 
+4
source share
1 answer

This was resolved by adding another field to the class to save a string version of the floor. The mapping changes to string mapping, and the SEXID field is ignored and set later during the DAL operation.

 CreateMap<StagingPerson, Person>() .ForMember( dest => dest.SexString, u => u.MapFrom( src => src.Sex) ) .ForMember( dest => dest.SEXID, u.Ignore() ) ); /// The source class public class StagingPerson { /// holds "M", "F", "U", "?", etc public string Gender {get;set;} } 

and

 /// the destination class public class Person { public int SEXID {get;set;} // New property to store the source string value // looked up in the Data Access Layer to determine the sexId public string SexString {get;set;} } 
0
source

Source: https://habr.com/ru/post/1491682/


All Articles