繁体   English   中英

按惯例自动定义自动定义,无需显式CreateMap

[英]Automapper definition by convention without explicit CreateMap

我正在尝试创建Automapper约定,以便在DTO与大写属性之间进行映射,使用下划线和业务POCO,这是pascal案例。

我想这样做而不为每个类显式调用CreateMap ,因为映射规则在任何地方都是相同的。

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; }
}

所以我像这样创建映射器。

var config = new MapperConfiguration(cfg =>
{
    cfg.AddProfile(new DtoToBusinessProfile());
});

IMapper mapper = new Mapper(config);

该个人资料看起来像这样。

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?
    }
}

我在Automapper github中创建了一个功能请求 ,因为我无法使INamingConvention接口工作

有人告诉我,通过使用ForAllMaps ,我想要的也是可能的,但我不知道如何处理它。

在Automapper中发现了一个错误 ,但也是一个有两个映射器的工作解决方案。 显然, 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM