如何在 .NetCore 中使用 AutoMapper 高级功能

开发 前端
AutoMapper 是一个基于约定的面向对象的映射器,它的功能常用于将一个 input 对象 转成一个不同类型的 output 对象,input 和 output 对象之间的属性可能相同也可能不相同,这一篇我们来一起研究一下 AutoMapper 的一些高级玩法。

[[375141]]

本文转载自微信公众号「码农读书」,作者码农读书。转载本文请联系码农读书公众号。

AutoMapper 是一个基于约定的面向对象的映射器,它的功能常用于将一个 input 对象 转成一个不同类型的 output 对象,input 和 output 对象之间的属性可能相同也可能不相同,这一篇我们来一起研究一下 AutoMapper 的一些高级玩法。

安装 AutoMapper

要想在项目中使用 AutoMapper ,需要通过 nuget 引用 AutoMapper 和 AutoMapper.Extensions.Microsoft.DependencyInjection 包,可以通过 Visual Studio 2019 的 NuGet package manager 可视化界面安装 或者 通过 NuGet package manager 命令行工具输入以下命令:

  1. Install-Package AutoMapper 
  2. Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection 

配置 AutoMapper

一旦 AutoMapper 成功安装之后,接下来就可以将它引入到 ServiceCollection 容器中,如下代码所示:

  1. public void ConfigureServices(IServiceCollection services) 
  2.         {          
  3.             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); 
  4.  
  5.             services.AddAutoMapper(typeof(AuthorProfile)); 
  6.         } 

使用 profiles 统一管理 mapping 信息

可以使用 profiles 来统一组织你的 mapping 信息,要创建 profile,需要实现 AutoMapper 提供的 Profile 类,然后在你刚才创建的 Profile 子类的构造函数中添加映射信息,下面的代码展示了如何创建一个从 Proifle 继承的 AuthorProfile 类以及相关信息。

  1. public class AuthorProfile : Profile 
  2.      public AuthorProfile() 
  3.      { 
  4.          CreateMap<AuthorModel, AuthorDTO>(); 
  5.      } 

接下来再看 AuthorModel 和 AuthorDTO 两个对象的定义:

  1. public class AuthorModel 
  2.    { 
  3.        public int Id 
  4.        { 
  5.            get; set
  6.        } 
  7.        public string FirstName 
  8.        { 
  9.            get;set
  10.        } 
  11.        public string LastName 
  12.        { 
  13.            get; set
  14.        } 
  15.        public string Address 
  16.        { 
  17.            get; set
  18.        } 
  19.    } 
  20.  
  21.    public class AuthorDTO 
  22.    { 
  23.        public int Id 
  24.        { 
  25.            get; set
  26.        } 
  27.        public string FirstName 
  28.        { 
  29.            get; set
  30.        } 
  31.        public string LastName 
  32.        { 
  33.            get; set
  34.        } 
  35.        public string Address 
  36.        { 
  37.            get; set
  38.        } 

使用 ReverseMap()

值得注意的是,上面的示例是一种 单向流动,这是什么意思呢?举个例子吧,下面是 单向流动 的一段代码。

  1. AutoMapper.Mapper.CreateMap<AuthorDTO, AuthorModel>(); 

有了这个 Map,接下来就可以轻松实现 AuthorDTO 到 AuthorModel 的转换,代码如下:

  1. var authorModel = AutoMapper.Mapper.Map<AuthorModel>(author); 

假设因为某种原因,你需要将 authorModel 实例反转成 authorDTO,这时你用了如下的代码段。

  1. var author = AutoMapper.Mapper.Map(authorModel); 

很遗憾,这种方式注定会抛出异常,这是因为 AutoMapper 并不知道如何实现 authorModel 到 authorDTO 的转换,毕竟你没有定义此种 map 的映射流向,那怎么解决呢?可以再定义一个 CreateMap 映射哈,其实没必要,简单粗暴的做法就是调用 ReverseMap 即可,实现代码如下:

  1. AutoMapper.Mapper.CreateMap<AuthorDTO, AuthorModel>().ReverseMap(); 

使用 ForMember() 和 MapFrom()

这一节我们继续使用之前说到的 AuthorModel 和 AuthorDTO 类,下面的代码片段展示了如何将 AuthorModel 转成 AuthorDTO 。

  1. var author = new AuthorModel();            
  2. author.Id = 1; 
  3. author.FirstName = "Joydip"
  4. author.LastName = "Kanjilal"
  5. author.Address = "Hyderabad"
  6. var authorDTO = _mapper.Map<AuthorDTO>(author); 

现在假设我将 AuthorModel 中的 Address 改成 Address1,如下代码所示:

  1. public class AuthorModel 
  2.    { 
  3.        public int Id 
  4.        { 
  5.            get; set
  6.        } 
  7.        public string FirstName 
  8.        { 
  9.            get; set
  10.        } 
  11.        public string LastName 
  12.        { 
  13.            get; set
  14.        } 
  15.        public string Address1 
  16.        { 
  17.            get; set
  18.        } 
  19.    } 

然后在 AuthorProfile 中更新一下 mapping 信息,如下代码所示:

  1. public class AuthorProfile : Profile 
  2.     { 
  3.         public AuthorProfile() 
  4.         { 
  5.             CreateMap<AuthorModel, AuthorDTO>().ForMember(destination => destination.Address, map => map.MapFrom(source => source.Address1)); 
  6.         } 
  7.     } 

使用 NullSubstitute

何为 NullSubstitute 呢?大意就是在映射转换的过程中,将input 为null 的属性映射之后做自定义处理,比如在 ouput 中改成 No Data,下面的代码展示了如何去实现。

  1. AutoMapper.Mapper.CreateMap<AuthorModel, AuthorDTO>().ForMember(destination => destination.Address, opt => opt.NullSubstitute("No data")); 

mapping 的 AOP 拦截

考虑下面的两个类。

  1. public class OrderModel 
  2.  { 
  3.    public int Id { get; set; } 
  4.    public string ItemCode { get; set; } 
  5.    public int NumberOfItems { get; set; } 
  6.  } 
  7.  
  8.  public class OrderDTO 
  9.  { 
  10.    public int Id { get; set; } 
  11.    public string ItemCode { get; set; } 
  12.    public int NumberOfItems { get; set; } 
  13.  } 

可以使用 BeforeMap() 在 源对象 或者 目标对象 上执行一些计算或者初始化成员操作,下面的代码展示了如何去实现。

  1. Mapper.Initialize(cfg => { 
  2.   cfg.CreateMap().BeforeMap((src, dest) => src.NumberOfItems = 0) 
  3. }); 

当 mapping 执行完之后,可以在 目标对象 上 安插 AfterMap() 方法,下面的代码展示了如何去实现。

  1. public OrderDTO MapAuthor(IMapper mapper, OrderDTO orderDTO) 
  2.         { 
  3.             return mapper.Map<OrderModel, OrderDTO>(orderDTO, opt => 
  4.             { 
  5.                 opt.AfterMap((src, dest) => 
  6.                 { 
  7.                     dest.NumberOfItems = _orderService.GetTotalItems(src); 
  8.                }); 
  9.             }); 
  10.         } 

使用嵌套映射

AutoMapper 同样也可以使用嵌套映射,考虑下面的 domain 类。

  1. public class Order 
  2.     { 
  3.         public string OrderNumber { get; set; } 
  4.         public IEnumerable<OrderItem> OrderItems { get; set; } 
  5.     } 
  6.  
  7.     public class OrderItem 
  8.     { 
  9.         public string ItemName { get; set; } 
  10.         public decimal ItemPrice { get; set; } 
  11.         public int ItemQuantity { get; set; } 
  12.     } 

接下来再看一下 DTO 类。

  1. public class OrderDto 
  2.     { 
  3.         public string OrderNumber { get; set; } 
  4.         public IEnumerable<OrderItemDto> OrderItems { get; set; } 
  5.     } 
  6.  
  7.     public class OrderItemDto 
  8.     { 
  9.         public string ItemName { get; set; } 
  10.         public decimal ItemPrice { get; set; } 
  11.         public int ItemQuantity { get; set; } 
  12.     } 

最后看看如何在转换的过程中使用 mapping 的。

  1. var orders = _repository.GetOrders(); 
  2. Mapper.CreateMap<Order, OrderDto>(); 
  3. Mapper.CreateMap<OrderItem, OrderItemDto>(); 
  4. var model = Mapper.Map<IEnumerable<Order>, IEnumerable<OrderDto>>(orders); 

AutoMapper 让你用最小化的配置实现了对象之间的映射,同时也可以实现自定义的解析器来实现具有完全不同结构对象之间的映射,自定义解析器可以生成与目标对象具有相同结构的exchange,以便AutoMapper在运行时可以据其实现映射。

译文链接:https://www.infoworld.com/article/3406800/more-advanced-automapper-examples-in-net-core.html

 

责任编辑:武晓燕 来源: 码农读书
相关推荐

2009-02-24 11:05:07

ibmdwiphonegoogle

2021-07-13 07:52:02

Linuxgrep命令

2021-07-20 10:00:28

Linuxgrep命令

2021-03-09 07:27:40

Kafka开源分布式

2015-08-27 09:46:09

swiftAFNetworkin

2022-05-17 08:25:10

TypeScript接口前端

2024-01-18 08:37:33

socketasyncio线程

2021-06-09 09:36:18

DjangoElasticSearLinux

2022-06-23 08:00:53

PythonDateTime模块

2011-08-10 09:31:41

Hibernateunion

2019-09-16 19:00:48

Linux变量

2020-11-30 11:55:07

Docker命令Linux

2020-04-09 10:18:51

Bash循环Linux

2014-07-02 09:47:06

SwiftCocoaPods

2021-11-02 13:54:41

ElasticSear.NET程序

2012-03-07 09:08:00

HTML 5

2023-12-01 09:18:27

AxiosAxios 库

2015-11-26 10:57:56

DockerOpen vSwitc

2021-09-10 10:30:22

Java代码

2022-10-25 09:07:28

Linuxxargs命令
点赞
收藏

51CTO技术栈公众号