我需要使用AutoMapper将对象映射到另一个对象。棘手的问题是如何在映射配置内或自定义类型转换器内部访问映射器实例(IMapper实例)?
下面的代码不起作用,但它是我想要实现的示例 - 请注意mapper.Map
次调用并假设定义了映射Customer => CustomerDto
和Customer => DetailedCustomerDto
。
var config = new MapperConfiguration(
cfg => cfg.CreateMap<Order, OrderDto>()
.ForMember(dst => dst.Customer, src => src.ResolveUsing(o => {
return o.Type == 1
? mapper.Map<Customer, CustomerDto>(o.Customer)
: mapper.Map<Customer, DetailedCustomerDto>(o.Customer)
})
);
客户端部分是:
var mapper = config.CreateMapper();
var orderDto = mapper.Map<Order, OrderDto>(order);
我要映射的对象的简化版本是:
public class Order
{
public int Type { get; set; }
public Customer Customer { get; set; }
}
public class Customer
{
public long Id { get; set; }
public string Name { get; set; }
}
public class OrderDto
{
public CustomerDto Customer { get; set; }
}
public class CustomerDto
{
public long Id { get; set; }
}
public class DetailedCustomerDto : CustomerDto
{
public string Name { get; set; }
}
从上面的代码中可以看出,基于Order.Type
的值,映射器应将属性Order.Customer
映射到不同的目标。当一个目标(DetailedCustomerDto
)继承自另一个目标(CustomerDto
)时,它变得有点棘手。
请注意,过时和弃用的静态方法Mapper.Map的使用不是一个选项。
答案 0 :(得分:42)
自AutoMapper 5.1.1起
您可以使用另外的ResolveUsing
重载来访问映射器,其中包含四个参数,其中第四个参数为ResolutionContext
(context.Mapper
):
var config = new MapperConfiguration(
cfg => {
cfg.CreateMap<Customer, CustomerDto>();
cfg.CreateMap<Customer, DetailedCustomerDto>();
cfg.CreateMap<Order, OrderDto>()
.ForMember(dst => dst.Customer, src => src.ResolveUsing((order, orderDto, i, context) => {
return order.Type == 1
? context.Mapper.Map<Customer, CustomerDto>(order.Customer)
: context.Mapper.Map<Customer, DetailedCustomerDto>(order.Customer);
}));
});
var orderTypeOne = new Order();
orderTypeOne.Type = 1;
orderTypeOne.Customer = new Customer() {
Id = 1
};
var dto = config.CreateMapper().Map<Order, OrderDto>(orderTypeOne);
Debug.Assert(dto.Customer.GetType() == typeof (CustomerDto));
var orderTypeTwo = new Order();
orderTypeTwo.Type = 2;
orderTypeTwo.Customer = new Customer() {
Id = 1
};
dto = config.CreateMapper().Map<Order, OrderDto>(orderTypeTwo);
Debug.Assert(dto.Customer.GetType() == typeof (DetailedCustomerDto));
在AutoMapper 5.1.1之前
您可以使用另外两个ResolveUsing
重载来访问映射器,其中包含两个参数,首先是ResolutionResult
(result.Context.Engine.Mapper
):
var config = new MapperConfiguration(
cfg => {
cfg.CreateMap<Customer, CustomerDto>();
cfg.CreateMap<Customer, DetailedCustomerDto>();
cfg.CreateMap<Order, OrderDto>()
.ForMember(dst => dst.Customer, src => src.ResolveUsing((result, order) => {
return order.Type == 1
? result.Context.Engine.Mapper.Map<Customer, CustomerDto>(order.Customer)
: result.Context.Engine.Mapper.Map<Customer, DetailedCustomerDto>(order.Customer);
}));
});
var orderTypeOne = new Order();
orderTypeOne.Type = 1;
orderTypeOne.Customer = new Customer() {
Id = 1
};
var dto = config.CreateMapper().Map<Order, OrderDto>(orderTypeOne);
Debug.Assert(dto.Customer.GetType() == typeof (CustomerDto));
var orderTypeTwo = new Order();
orderTypeTwo.Type = 2;
orderTypeTwo.Customer = new Customer() {
Id = 1
};
dto = config.CreateMapper().Map<Order, OrderDto>(orderTypeTwo);
Debug.Assert(dto.Customer.GetType() == typeof (DetailedCustomerDto));
答案 1 :(得分:6)
除了Evk的好答案,这对我有帮助,如果你需要在需要自定义构造函数的配置/配置文件中的映射内部进行映射(即类型没有默认构造函数),将在v5.2.0中运行:
CreateMap<Models.Job, Models.API.Job>(MemberList.Source);
CreateMap<StaticPagedList<Models.Job>, StaticPagedList<Models.API.Job>>()
.ConstructUsing((source, context) => new StaticPagedList<Models.API.Job>(
context.Mapper.Map<List<Models.Job>, List<Models.API.Job>>(source.ToList()),
source.PageNumber,
source.PageSize,
source.TotalItemCount));
在此示例中,我将一个对象类型的X.PagedList自定义集合类型映射到另一个对象类型的等效集合。 lamdba表达式的第一个参数是源对象,第二个参数是ResolutionContext
,您可以从中访问要映射的映射器实例。
答案 2 :(得分:4)
我正在使用Automapper 9
,上面的答案对我不起作用。
然后使用.afterMap
来解决您的问题,例如:
public class AutoMapperOrder : Profile
{
public AutoMapperOrder()
{
CreateMap<Customer, CustomerDto>()
//...
CreateMap<Customer, DetailedCustomerDto>()
//...
CreateMap<Order, OrderDto>()
.AfterMap((src, dest, context) => {
dest.Customer = src.Type == 1
? context.Mapper.Map<Customer, CustomerDto>(src.Customer)
: context.Mapper.Map<Customer, DetailedCustomerDto>(src.Customer)
}
}
}
}
我希望能帮助别人。