AutoMapper搬运工之初探AutoMapper

时间:2023-03-09 17:43:45
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对象中:

 public class Order
{
  private readonly IList<OrderLineItem> _orderLineItems = new List<OrderLineItem>();   public Customer Customer { get; set; }   public OrderLineItem[] GetOrderLineItems()
  {
    return _orderLineItems.ToArray();
  }   public void AddOrderLineItem(Product product, int quantity)
  {
    _orderLineItems.Add(new OrderLineItem(product, quantity));
  }   public decimal GetTotal()
  {
    return _orderLineItems.Sum(li => li.GetTotal());
  }
}
} public class OrderLineItem
{
  public OrderLineItem(Product product, int quantity)
  {
    Product = product;
    Quantity = quantity;
  }   public Product Product { get; private set; }
  public int Quantity { get; private set;}   public decimal GetTotal()
  {
    return Quantity*Product.Price;
  }
} public class Customer
{
  public string Name { get; set; }
} public class Product
{
  public decimal Price { get; set; }
  public string Name { get; set; }
}

  然后在页面上我需要展示一些数据,为了保持页面ViewModel的独立,定义一个DTO(OrderDto)只包含了页面需要的字段,如下:

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

然后问题来了,怎么转换呢?

我想最简单的方法肯定是这样子:

//定义一个order
var customer = new Customer
{
Name = "George Costanza";
}
var order = new Order
{
Customer = customer;
}
var bosco = new Product
{
Name = "Bosco",
Price = 4.99m;
}
order.AddOrderLineItem(bosco, ); //转换
var dto = new OrderDto();
dto.CustomerName = order.Customer.Name;
dto.Total = order.GetTotal();

上面的代码看起来很简单没什么问题,但是如果dto属性有很多个呢?反复的手动Mapping会不会有点累?这时候攻城狮就得发挥偷懒精神,以下是使用了AutoMapper的代码:

// 配置一个 AutoMapper 映射

Mapper.CreateMap<Order, OrderDto>();

// 执行 mapping
OrderDto 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之自定义映射》里,我会为大家搬运。有啥疑问和意见可以给我留言,咱们来探讨一番,请大拿不吝赐教~!