15. AutoMapper 之映射继承(Mapping Inheritance)

映射继承(Mapping Inheritance)

映射继承有两个功能:

  • 从基类或接口配置继承映射配置
  • 运行时多态映射

继承的配置是可选择的,你可以在基类中使用Include 来指定可被继承的映射配置或者在派生类使用IncludeBase来指定:

CreateMap<BaseEntity, BaseDto>()
   .Include<DerivedEntity, DerivedDto>()
   .ForMember(dest => dest.SomeMember, opt => opt.MapFrom(src => src.OtherMember));

CreateMap<DerivedEntity, DerivedDto>();

或者

CreateMap<BaseEntity, BaseDto>()
   .ForMember(dest => dest.SomeMember, opt => opt.MapFrom(src => src.OtherMember));

CreateMap<DerivedEntity, DerivedDto>()
    .IncludeBase<BaseEntity, BaseDto>();

在上面的情况中,派生的映射继承于基础映射配置中的自定义映射配置。

运行时多态

用法:

public class Order { }
public class OnlineOrder : Order { }
public class MailOrder : Order { }

public class OrderDto { }
public class OnlineOrderDto : OrderDto { }
public class MailOrderDto : OrderDto { }

Mapper.Initialize(cfg => {
    cfg.CreateMap<Order, OrderDto>()
        .Include<OnlineOrder, OnlineOrderDto>()
        .Include<MailOrder, MailOrderDto>();
    cfg.CreateMap<OnlineOrder, OnlineOrderDto>();
    cfg.CreateMap<MailOrder, MailOrderDto>();
});

// 执行映射
var order = new OnlineOrder();
var mapped = Mapper.Map(order, order.GetType(), typeof(OrderDto));
Assert.IsType<OnlineOrderDto>(mapped);

需要映射的对象是OnlineOrderAutoMapper找到一个比OrderDto更具体的OnlineOrder映射,就自动选择了OnlineOrder

在派生类中指定继承

你可以使用在派生类中指定需要继承的映射来代替在基类中配置需要被继承的映射:

Mapper.Initialize(cfg => {
  cfg.CreateMap<Order, OrderDto>()
    .ForMember(o => o.Id, m => m.MapFrom(s => s.OrderId));
  cfg.CreateMap<OnlineOrder, OnlineOrderDto>()
    .IncludeBase<Order, OrderDto>();
  cfg.CreateMap<MailOrder, MailOrderDto>()
    .IncludeBase<Order, OrderDto>();
});

继承映射优先级

一个属性有多种映射途径,它们的优先级如下:

  • 显式映射 (使用 .MapFrom())
  • 继承的显式映射
  • 忽略属性映射
  • 转换映射 (匹配的通过转换的属性)

为了演示,我们修改上面显示的类

//域对象
public class Order { }
public class OnlineOrder : Order
{
    public string Referrer { get; set; }
}
public class MailOrder : Order { }

//数据传输对象
public class OrderDto
{
    public string Referrer { get; set; }
}

//映射
Mapper.Initialize(cfg => {
    cfg.CreateMap<Order, OrderDto>()
        .Include<OnlineOrder, OrderDto>()
        .Include<MailOrder, OrderDto>()
        .ForMember(o=>o.Referrer, m=>m.Ignore());
    cfg.CreateMap<OnlineOrder, OrderDto>();
    cfg.CreateMap<MailOrder, OrderDto>();
});

// 执行映射
var order = new OnlineOrder { Referrer = "google" };
var mapped = Mapper.Map(order, order.GetType(), typeof(OrderDto));
Assert.Equals("google", mapped.Referrer);

请注意,在的映射配置中,我们忽略了Referrer(因为在order的基类中找不到),而且它有一个更高优先级的转换映射,所以这个成员没有被映射。

总得来说,这个特征使得AutoMapper使用继承变得更自然。

推荐阅读更多精彩内容