AutoMapper 实现DTO和领域Model之间的转换

首先需要在NuGet中引用AutoMapper的类库
声明:
若class类型参数一致则:
Mapper.Initialize(x => x.CreateMap<原始Class, 须赋值Class>()));
若扩展参数:
Mapper.Initialize(x => x.CreateMap<business_Labi_CourseEnrollOrder, business_Labi_CourseEnrollOrderDto>().ForMember(y=>y.OrderDetails,dto=>dto.MapFrom(s=>s.OrderDetails.Select(t=>new business_Labi_CourseEnrollOrderDto.OrderDetail
                {
                    ProductVersionID = t.ProductVersionID,
                    Qty = t.Qty,
                    SalePrice = t.SalePrice,
                }))));
 调用:
var info = Mapper.Map<business_Labi_CourseEnrollOrder, business_Labi_CourseEnrollOrderDto>(item);
posted @ 2018-02-27 14:37  wulala9  阅读(271)  评论(0编辑  收藏  举报