32450新蒲京网站AutoMapper使用轻便计算

2019-11-26 15:01 来源:未知

如果Mapper使用不当,会出现如下异常:

 

在vs的NuGet程序包控制台install AutoMapper。

(4)包含子对象的对象映射,需要先映射子对象

  1. 关于NuGet的升级

项目地址:。

接触AutoMapper已经有两年多的时间了,AutoMapper足够轻量级,而且也非常流行,国外的大牛们都使用它。使用AutoMapper可以很方便的实现DTO和领域Model之间的转换,它是一个强大的Object-Object Mapping工具。在ORM框架中,它使持久层对象与DTO对象之间的转换变得相当简单。 随着负责的项目的增多,使用的技术框架一多起来,很多具体的技术点难免记不清, 加上同时兼做需求、研发、质量管控, 有时候写起代码来就感到很手生。 好脑袋不如烂笔头,如下,简单整理一下。

  1. Demo下载

AutoMapper是基于约定的,因此在实用映射之前,我们需要先进行映射规则的配置。

  1. 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; }
     }
    
Mapper.CreateMap<BizDatOrders, AdjTotalBill>()
    .ForMember(dto => dto.CreatedTime, opt => opt.Ignore())
    .ForMember(dest => dest.Passenger, map => map.MapFrom(m => m.PsgName)) // 乘机人姓名
    .ForMember(dest => dest.BookMan, (map) => map.MapFrom(m => m.BookAccount))
    .ForMember(dest => dest.DistributorProfit, map => map.MapFrom(m => m.DisProfit))
    .ForMember(dest => dest.PlatformProfit, map => map.MapFrom(m => m.CenProfit));// 预订人

var one = Mapper.Map<AdjTotalBill>(item);

 

我vs2013的NuGet的版本是2.7,因为要装AutoMapper4.1,要升级到2.8。升级方法:

(1) 属性类型和名称相同的对象之间直接映射

32450新蒲京网站 1

3.其他说明

32450新蒲京网站 2

Mapper.Reset();

(提示:貌似要在安装前先重启vs)

 或者也可以这样写

  1. 常见异常
  1. 参考网址
  1. 安装AutoMapper

 

  1. vs2013里“工具”→“扩展和更新”里,在本机已安装里找到NuGet Package Manager(NuGet程序包管理器),卸载。

  2. 在联机里,搜索NuGet,安装即可。

(5)转化存在的对象

  1. 使用

(2)重新配置映射 

vs扩展产品见微软官网。 像ReShaper、TestCaseGenerator,都不错。

 

NuGet 是 Visual Studio 的扩展工具,在使用Visual Studio开发基于.NET Framework的应用时,Nuget能把在项目中添加、移除和更新引用的工作变得更加快捷方便。

1. AutoMapper是一个.NET的对象映射工具,可以方便地进行对象间的赋值处理.

Missing type map configuration or unsupported mapping.

Mapping types:
CouponUsageDTO -> CouponUsage
Coupon.Domain.DTO.CouponUsageDTO -> Coupon.Domain.Entities.CouponUsage

Destination path:
CouponUsage

Source value:
Coupon.Domain.DTO.CouponUsageDTO

    近期,在用AutoMapper整理一些模型对象映射,顺便小结一下使用的体会。难免有写得不对的地方,谢谢指出!

 

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())
              //包含多个相同子对象时
              .ForMember(dest => dest.OrderDetaiEntity, opt => opt.MapFrom(src =>
                        Mapper.Map<OrderDetail, OrderDetailDto>(src.OrderDetaiEntity)
                  ));

AutoMapper.Mapper.AssertConfigurationIsValid();

 

 32450新蒲京网站 3

或者可以这样写

如可以实现  TypeConverter, ValueResolver,ValueFormatter  抽象方法.

 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));
  1. 对象映射情况

 

 

 

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

 

 

Mapper.CreateMap<ProductDetail, ProductDetailDto>();
ProductDetail product = new ProductDetail(){ //赋值 };
ProductDetailDto productDto = Mapper.Map<ProductDetail,ProductDetailDto>(product);
 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);

(6)其他的, 通过自定义转化来实现

(2) 对象之间属性转换、属性忽略

 

(3)属性填充固定值

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

 

 

 AutoMapperSolution.rar

(1)检查还有那些规则没有写完整,可以在最后调用下面的方法

帮助文档:

TAG标签:
版权声明:本文由32450新蒲京网站发布于葡萄游戏厅_体育游戏,转载请注明出处:32450新蒲京网站AutoMapper使用轻便计算