国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

AutoMapper搬運工之初探AutoMapper

2019-11-17 02:49:59
字體:
來源:轉載
供稿:網友

AutoMapper搬運工之初探AutoMapper

寫在前面

知道AutoMapper很久了,但是一直沒有用,最近剛好有個場景需要就用了,果然是利器。看了git上的wiki,發現內容其實wiki上寫的很全面了,深入的暫時還沒挖掘到。不過和群里的朋友交流了下,覺得充當下搬運工,小小的翻譯下還是挺好的,小弟不才,希望看客大牛們輕拍磚。

什么是AutoMapper?

AutoMapper是一個完善的Mapping工具,所謂Mapping即數據對象轉換,借用群里朋友的話是多用于領域對象和DTO對象的Mapping或者是SqlDataReader的Mapping。

Git主頁:https://github.com/AutoMapper/AutoMapper

Wiki主頁:https://github.com/AutoMapper/AutoMapper/wiki

簡單的例子

場景是這樣的在訂單業務里,有Order、Customer、PRoduct三個對象如下,其中Product被包含在OrderLinItem對象中:

 1 public class Order 2 { 3   private readonly IList<OrderLineItem> _orderLineItems = new List<OrderLineItem>(); 4  5   public Customer Customer { get; set; } 6  7   public OrderLineItem[] GetOrderLineItems() 8   { 9     return _orderLineItems.ToArray();10   }11 12   public void AddOrderLineItem(Product product, int quantity)13   {14     _orderLineItems.Add(new OrderLineItem(product, quantity));15   }16 17   public decimal GetTotal()18   {19     return _orderLineItems.Sum(li => li.GetTotal());20   }21 }22 }23 24 public class OrderLineItem25 {26   public OrderLineItem(Product product, int quantity)27   {28     Product = product;29     Quantity = quantity;30   }31 32   public Product Product { get; private set; }33   public int Quantity { get; private set;}34 35   public decimal GetTotal()36   {37     return Quantity*Product.Price;38   }39 }40 41 public class Customer42 {43   public string Name { get; set; }44 }45 46 public class Product47 {48   public decimal Price { get; set; }49   public string Name { get; set; }50 }
View Code

  然后在頁面上我需要展示一些數據,為了保持頁面ViewModel的獨立,定義一個DTO(OrderDto)只包含了頁面需要的字段,如下:

public class OrderDto{    public string CustomerName { get; set; }    public decimal Total { get; set; }}
View Code

然后問題來了,怎么轉換呢?

我想最簡單的方法肯定是這樣子:

//定義一個ordervar customer = new Customer{    Name = "George Costanza";}var order = new Order{    Customer = customer;}var bosco = new Product{    Name = "Bosco",    Price = 4.99m;}order.AddOrderLineItem(bosco, 15);//轉換var dto = new OrderDto();dto.CustomerName = order.Customer.Name;dto.Total = order.GetTotal();

上面的代碼看起來很簡單沒什么問題,但是如果dto屬性有很多個呢?反復的手動Mapping會不會有點累?這時候攻城獅就得發揮偷懶精神,以下是使用了AutoMapper的代碼:

// 配置一個 AutoMapper 映射Mapper.CreateMap<Order, OrderDto>();// 執行 mappingOrderDto dto = Mapper.Map<Order, OrderDto>(order);

是不是很簡單簡潔了,但是細心的會發現,這個Mapping稍微有點復雜,CustomerName對應的是order.Customer.Name;Total對應的是GetTotal方法。AutoMaper是如何做到,又是如何定義這個規則的呢?

AutoMapper的簡單默認Mapping規則

1. 針對復雜對象到簡單對象的映射,Mapping遵循駝峰命名的規則,即當目標dto屬性名稱在源對象中找不到時,AutoMapper會根據駝峰命名規則拆分dto屬性名(例如:CustomerName=>Customer.Name)去尋找相應的關系。

2. 針對是源對象中存在Get開頭的屬性(例如 Total => GetTotal),如果找不到同名屬性,則會匹配Get+PropertyName的方法。

本文搬運自AutoMapper Wiki之Flattening:https://github.com/AutoMapper/AutoMapper/wiki/Flattening

上面的規則是最簡單的匹配規則,對于復雜的Mapping,完全不符合上面的兩條特性的,那么就需要進行Mapping的自定義配置,下節 《AutoMapper之自定義映射》里,我會為大家搬運。有啥疑問和意見可以給我留言,咱們來探討一番,請大拿不吝賜教~!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邢台市| 天门市| 克什克腾旗| 建平县| 屯留县| 金平| 舟山市| 江阴市| 南溪县| 安福县| 温宿县| 成武县| 晋城| 石景山区| 双江| 宁陵县| 黄梅县| 临汾市| 桃源县| 久治县| 灵璧县| 来宾市| 新龙县| 临湘市| 玉环县| 家居| 南华县| 临湘市| 八宿县| 南康市| 新竹市| 陆川县| 柏乡县| 龙井市| 庄河市| 凉山| 兴城市| 疏附县| 钟祥市| 新昌县| 龙江县|