[英]Automapper definition by convention without explicit CreateMap
I'm trying to create Automapper conventions for mapping between DTO's with uppercase properties with underscores and business POCO's which are pascal case. 我正在尝试创建Automapper约定,以便在DTO与大写属性之间进行映射,使用下划线和业务POCO,这是pascal案例。
I want to do this without explicitly calling CreateMap
for each class since the rules of mapping are same everywhere. 我想这样做而不为每个类显式调用
CreateMap
,因为映射规则在任何地方都是相同的。
DTO's are prefixed with "T_". DTO的前缀是“T_”。
// DTO
public class T_ACCOUNT
{
public int ID { get; set; }
public int? PROFILE_ID { get; set; }
public bool DELETED { get; set; }
public string EMAIL { get; set; }
public bool IS_EMAIL_CONFIRMED { get; set; }
public DateTime TIME { get; set; }
}
// business model
public class Account
{
public int Id { get; set; }
public int? ProfileId { get; set; }
public bool Deleted { get; set; }
public string Email { get; set; }
public bool IsEmailConfirmed { get; set; }
public DateTime Time { get; set; }
}
So I create the mapper like this. 所以我像这样创建映射器。
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new DtoToBusinessProfile());
});
IMapper mapper = new Mapper(config);
The profile looks like this. 该个人资料看起来像这样。
public class DtoToBusinessProfile : Profile
{
public DtoToBusinessProfile()
{
// this should match mapping between "Account" and "T_ACCOUNT"
AddConditionalObjectMapper()
.Where((s, d) =>
{
return s.Name == d.Name.Substring(2).Pascalize();
});
// Now what?
}
}
I created a feature request in Automapper github since I could not get the INamingConvention
interface to work . 我在Automapper github中创建了一个功能请求 ,因为我无法使
INamingConvention
接口工作 。
I was told that what I want is also possible by using ForAllMaps
but I have no idea what to do with it. 有人告诉我,通过使用
ForAllMaps
,我想要的也是可能的,但我不知道如何处理它。
Found a bug in Automapper but also a working solution with two mappers. 在Automapper中发现了一个错误 ,但也是一个有两个映射器的工作解决方案。 Apparently the
LowerUnderscoreNamingConvention
works well also with uppercase properties. 显然,
LowerUnderscoreNamingConvention
也适用于大写属性。
IMapper dtoToBusinessMapper = new MapperConfiguration(cfg =>
{
cfg.AddConditionalObjectMapper()
.Where((s, d) => s.Name == d.Name.Substring(2).Pascalize());
cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
}).CreateMapper();
IMapper businessToDtoMapper = new MapperConfiguration(cfg =>
{
cfg.AddConditionalObjectMapper()
.Where((s, d) => s.Name == "T_" + d.Name.Underscore().ToUpperInvariant());
cfg.SourceMemberNamingConvention = new PascalCaseNamingConvention();
cfg.DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention();
}).CreateMapper();
T_ACCOUNT tableRow = new T_ACCOUNT()
{
DELETED = 0,
EMAIL = "asdf@asdf.cz",
ID = 8,
IS_EMAIL_CONFIRMED = 1,
PROFILE_ID = 11,
TIME = DateTime.Now
};
Account businessModel = dtoToBusinessMapper.Map<Account>(tableRow);
T_ACCOUNT backToDto = businessToDtoMapper.Map<T_ACCOUNT>(businessModel);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.