近期,在用AutoMapper整理一些模型对象映射,顺便小结一下使用的体会。难免有写得不对的地方,谢谢指出!
1. AutoMapper是一个.NET的对象映射工具,可以方便地进行对象间的赋值处理.
项目地址:https://github.com/AutoMapper/AutoMapper。
帮助文档:https://github.com/AutoMapper/AutoMapper/wiki
2. Demo中使用到的类
public class Order
{
public int Id { get; set; }
public Decimal? TotalAmount { get; set; }
public bool? IsEnable { get; set; } public OrderDetail OrderDetaiEntity { get; set; } public List<ProductDetail> ProductList { get; set; }
} public class OrderDetail
{
public string Remark { get; set; } public DateTime? OrderDate { get; set; }
public string OrderUser { get; set; }
public DateTime? ClosedDate { get; set; } } public class ProductDetail
{
public int? Id { get; set; }
public string ProductName { get; set; }
public Decimal? Price { get; set; }
} public class OrderDto
{
public int Id { get; set; }
public Decimal TotalAmount { get; set; }
public bool IsEnable { get; set; } public string Remark { get; set; } public OrderDetailDto OrderDetaiEntity { get; set; } public List<ProductDetailDto> ProductList { get; set; }
} public class OrderDetailDto
{
public DateTime OrderDate { get; set; }
public string OrderUser { get; set; }
public DateTime? ClosedDate { get; set; }
} public class ProductDetailDto
{
public int? Id { get; set; }
public string ProductName { get; set; }
public Decimal? Price { get; set; }
}
2. 对象映射情况
(1) 属性类型和名称相同的对象之间直接映射
Mapper.CreateMap<ProductDetail, ProductDetailDto>();
ProductDetail product = new ProductDetail(){ //赋值 };
ProductDetailDto productDto = Mapper.Map<ProductDetail,ProductDetailDto>(product);
(2) 对象之间属性转换、属性忽略
Mapper.CreateMap<Order, OrderDto>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.TotalAmount, opt => opt.MapFrom(src => src.TotalAmount ?? 0))
.ForMember(dest => dest.IsEnable, opt => opt.MapFrom(src => src.IsEnable ?? false))
.ForMember(dest => dest.Remark, opt => opt.MapFrom(src => src.OrderDetaiEntity.Remark))
.ForMember(dest => dest.OrderDetaiEntity, opt => opt.Ignore())
.ForMember(dest => dest.ProductList, opt => opt.Ignore());
OrderDto orderDto1 = Mapper.Map<Order, OrderDto>(orderObj);
(3)属性填充固定值
Mapper.CreateMap<OrderDetail, OrderDetailDto>()
.ForMember(dest => dest.OrderDate, opt => opt.UseValue<DateTime>(DateTime.Now));
(4)包含子对象的对象映射,需要先映射子对象
Mapper.CreateMap<OrderDetail, OrderDetailDto>()
.ForMember(dest => dest.OrderDate, opt => opt.UseValue<DateTime>(DateTime.Now));
Mapper.CreateMap<ProductDetail, ProductDetailDto>(); Mapper.CreateMap<Order, OrderDto>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.TotalAmount, opt => opt.MapFrom(src => src.TotalAmount ?? 0))
.ForMember(dest => dest.IsEnable, opt => opt.MapFrom(src => src.IsEnable ?? false))
.ForMember(dest => dest.Remark, opt => opt.MapFrom(src => src.OrderDetaiEntity.Remark));
或者可以这样写
Mapper.CreateMap<ProductDetail, ProductDetailDto>(); var exp = Mapper.CreateMap<Order, OrderDto>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.TotalAmount, opt => opt.MapFrom(src => src.TotalAmount ?? 0))
.ForMember(dest => dest.IsEnable, opt => opt.MapFrom(src => src.IsEnable ?? false))
.ForMember(dest => dest.Remark, opt => opt.MapFrom(src => src.OrderDetaiEntity.Remark));
//映射子对象
exp.ForMember(dest => dest.OrderDetaiEntity, opt => opt.MapFrom(dto => new OrderDetailDto() {
OrderDate = DateTime.Now,
OrderUser = dto.OrderDetaiEntity.OrderUser,
ClosedDate = dto.OrderDetaiEntity.ClosedDate
}));
或者也可以这样写
Mapper.CreateMap<OrderDetail, OrderDetailDto>()
.ForMember(dest => dest.OrderDate, opt => opt.UseValue<DateTime>(DateTime.Now));
Mapper.CreateMap<ProductDetail, ProductDetailDto>(); Mapper.CreateMap<Order, OrderDto>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.TotalAmount, opt => opt.MapFrom(src => src.TotalAmount ?? ))
.ForMember(dest => dest.Remark, opt => opt.Ignore())
.ForMember(dest => dest.IsEnable, opt => opt.Ignore())
//包含多个相同子对象时
.ForMember(dest => dest.OrderDetaiEntity, opt => opt.MapFrom(src =>
Mapper.Map<OrderDetail, OrderDetailDto>(src.OrderDetaiEntity)
));
(5)转化存在的对象
Mapper.CreateMap<OrderDetail, OrderDetailDto>()
.ForMember(dest => dest.OrderDate, opt => opt.UseValue<DateTime>(DateTime.Now));
Mapper.CreateMap<ProductDetail, ProductDetailDto>(); Mapper.CreateMap<Order, OrderDto>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.TotalAmount, opt => opt.MapFrom(src => src.TotalAmount ?? 0))
.ForMember(dest => dest.Remark, opt => opt.Ignore())
.ForMember(dest => dest.IsEnable, opt => opt.Ignore()); var orderDto4 = new OrderDto() { Remark = "this is default value.", IsEnable = true };
Mapper.Map<Order, OrderDto>(orderObj, orderDto4);
(6)其他的, 通过自定义转化来实现
如可以实现 TypeConverter, ValueResolver,ValueFormatter 抽象方法.
3.其他说明
(1)检查还有那些规则没有写完整,可以在最后调用下面的方法
AutoMapper.Mapper.AssertConfigurationIsValid();
(2)重新配置映射
Mapper.Reset();
4. 参考网址
http://www.cnblogs.com/ljzforever/archive/2011/12/29/2305500.html
http://www.cnblogs.com/jiguixin/archive/2011/09/19/2181521.html
http://www.cnblogs.com/dudu/archive/2011/12/16/2284828.html
5. Demo下载