日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何在.NetCore中使用AutoMapper高級功能

本文轉(zhuǎn)載自微信公眾號「碼農(nóng)讀書」,作者碼農(nóng)讀書。轉(zhuǎn)載本文請聯(lián)系碼農(nóng)讀書公眾號。

創(chuàng)新互聯(lián)建站從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站設(shè)計制作、做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元留壩做網(wǎng)站,已為上家服務(wù),為留壩各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792

AutoMapper 是一個基于約定的面向?qū)ο蟮挠成淦?,它的功能常用于將一個 input 對象 轉(zhuǎn)成一個不同類型的 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 統(tǒng)一管理 mapping 信息

可以使用 profiles 來統(tǒng)一組織你的 mapping 信息,要創(chuàng)建 profile,需要實現(xiàn) AutoMapper 提供的 Profile 類,然后在你剛才創(chuàng)建的 Profile 子類的構(gòu)造函數(shù)中添加映射信息,下面的代碼展示了如何創(chuàng)建一個從 Proifle 繼承的 AuthorProfile 類以及相關(guān)信息。

 
 
 
  1. public class AuthorProfile : Profile 
  2.      public AuthorProfile() 
  3.      { 
  4.          CreateMap(); 
  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(); 

有了這個 Map,接下來就可以輕松實現(xiàn) AuthorDTO 到 AuthorModel 的轉(zhuǎn)換,代碼如下:

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

假設(shè)因為某種原因,你需要將 authorModel 實例反轉(zhuǎn)成 authorDTO,這時你用了如下的代碼段。

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

很遺憾,這種方式注定會拋出異常,這是因為 AutoMapper 并不知道如何實現(xiàn) authorModel 到 authorDTO 的轉(zhuǎn)換,畢竟你沒有定義此種 map 的映射流向,那怎么解決呢?可以再定義一個 CreateMap 映射哈,其實沒必要,簡單粗暴的做法就是調(diào)用 ReverseMap 即可,實現(xiàn)代碼如下:

 
 
 
  1. AutoMapper.Mapper.CreateMap().ReverseMap(); 

使用 ForMember() 和 MapFrom()

這一節(jié)我們繼續(xù)使用之前說到的 AuthorModel 和 AuthorDTO 類,下面的代碼片段展示了如何將 AuthorModel 轉(zhuǎn)成 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(author); 

現(xiàn)在假設(shè)我將 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().ForMember(destination => destination.Address, map => map.MapFrom(source => source.Address1)); 
  6.         } 
  7.     } 

使用 NullSubstitute

何為 NullSubstitute 呢?大意就是在映射轉(zhuǎn)換的過程中,將input 為null 的屬性映射之后做自定義處理,比如在 ouput 中改成 No Data,下面的代碼展示了如何去實現(xiàn)。

 
 
 
  1. AutoMapper.Mapper.CreateMap().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() 在 源對象 或者 目標(biāo)對象 上執(zhí)行一些計算或者初始化成員操作,下面的代碼展示了如何去實現(xiàn)。

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

當(dāng) mapping 執(zhí)行完之后,可以在 目標(biāo)對象 上 安插 AfterMap() 方法,下面的代碼展示了如何去實現(xiàn)。

 
 
 
  1. public OrderDTO MapAuthor(IMapper mapper, OrderDTO orderDTO) 
  2.         { 
  3.             return mapper.Map(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 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 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.     } 

最后看看如何在轉(zhuǎn)換的過程中使用 mapping 的。

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

AutoMapper 讓你用最小化的配置實現(xiàn)了對象之間的映射,同時也可以實現(xiàn)自定義的解析器來實現(xiàn)具有完全不同結(jié)構(gòu)對象之間的映射,自定義解析器可以生成與目標(biāo)對象具有相同結(jié)構(gòu)的exchange,以便AutoMapper在運行時可以據(jù)其實現(xiàn)映射。

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


本文名稱:如何在.NetCore中使用AutoMapper高級功能
文章出自:http://m.5511xx.com/article/ccdgepp.html