成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何在 .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-20 10:00:28

Linuxgrep命令

2021-07-13 07:52:02

Linuxgrep命令

2019-09-16 19:00:48

Linux變量

2020-11-30 11:55:07

Docker命令Linux

2014-07-02 09:47:06

SwiftCocoaPods

2020-04-09 10:18:51

Bash循環Linux

2024-09-06 11:34:15

RustAI語言

2021-11-02 13:54:41

ElasticSear.NET程序

2022-05-17 08:25:10

TypeScript接口前端

2022-06-23 08:00:53

PythonDateTime模塊

2021-06-09 09:36:18

DjangoElasticSearLinux

2021-03-09 07:27:40

Kafka開源分布式

2015-08-27 09:46:09

swiftAFNetworkin

2024-01-18 08:37:33

socketasyncio線程

2011-08-10 09:31:41

Hibernateunion

2012-03-07 09:08:00

HTML 5

2021-01-04 05:44:54

框架日志

2018-05-16 10:32:06

Linux命令find

2018-06-26 09:15:24

Linux命令history
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 99久久免费精品视频 | 国精产品一区二区三区 | 欧美激情久久久 | 国产激情在线播放 | 日韩一区二区成人 | 成人免费视频网 | 成人av电影网 | 国产一区二区三区免费观看视频 | 国产精品永久免费观看 | 国产精品一区在线观看 | 久久偷人 | 亚洲精品福利视频 | 九九久久精品 | 中文字幕中文字幕 | 日韩精品一区二区三区中文在线 | 特一级毛片| 欧美日日 | 免费成人高清在线视频 | 三级视频在线观看 | 日韩av一二三区 | 欧美三级久久久 | 国产精品久久久久久久久久久久冷 | 青青操91| 91精品观看 | 精品99在线 | 色播av| 日韩久久精品 | 久草在线影 | 蜜桃一区二区三区 | 一级欧美黄色片 | 国产色播av在线 | 国产精品日韩高清伦字幕搜索 | 亚洲精品自在在线观看 | 色欧美综合 | 亚洲综合在线一区二区 | 午夜黄色影院 | 成人在线视频免费观看 | 一区二区三区亚洲视频 | 91欧美精品成人综合在线观看 | 中文字幕 国产精品 | 亚洲精品视 |