当需要查找映射值时,是否有另一种方法来处理调用数据库?在映射逻辑中进行数据库调用似乎很难看。这还怎么处理?
例如,一个表存储一个 SEXID 所以使用下面的解析器我可以做这样的事情
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;
}
}
数据访问:
public int GetGenderByString(string gender)
{
string sql = "select sexid from Sexes where sex = '" + gender "'";
int sexid = .....
......
return sexid;
}