本篇AutoMapper使用场景:
※ 源字典集合转换成目标字典集合
※ 枚举映射
※ 自定义解析器
※ 源中的复杂属性和Get...方法转换成目标属性
源字典集合转换成目标字典集合
□ Domain model
public class SourceValue
{
public int Value { get; set; }
}
□ View model
public class DestValue
{
public int Value { get; set; }
}
□ 映射配置
Mapper.CreateMap<SourceValue, DestValue>();
□ 使用
public ActionResult Dic()
{
var sourceDict = new Dictionary<string, SourceValue>
{
{"First", new SourceValue(){Value = 5}},
{"Second",new SourceValue(){Value = 10}}
};
var destDict = Mapper.Map<Dictionary<string, SourceValue>, IDictionary<String, DestValue>>(sourceDict);
return View((destDict));
}
枚举映射
public enum OrderStatus : short
{
InProgress = 0,
Complete = 1
}
public enum OrderStatusDto
{
InProgress = 0,
Complete = 1
}
□ 使用
//例子1
public ActionResult Meiju()
{
var dest = Mapper.Map<OrderStatus, OrderStatusDto>(OrderStatus.InProgress);
return View(dest);
}
//例子2
public ActionResult Meiju1()
{
var dest =
Mapper.Map<AttributeTargets, AttributeTargets>(AttributeTargets.Class | AttributeTargets.Interface);
return View(dest);
}
□ 要点
枚举映射不需要做映射配置。
自定义解析器
□ Domain model
public class Source1
{
public int Value { get; set; }
public int Value2 { get; set; }
}
□ View model
public class Destination1
{
public int Total { get; set; }
}
□ 自定义解析器,继承于ValueResolver<,>
public class CustomResolver : ValueResolver<Source1,int>
{
protected override int ResolveCore(Source1 source)
{
return source.Value + source.Value2;
}
}
□ 映射配置
Mapper.CreateMap<Source1, Destination1>()
.ForMember("Total", opt => opt.ResolveUsing<CustomResolver>());
□ 使用
public ActionResult Jiexi()
{
var source = new Source1()
{
Value = 5,
Value2 = 7
};
var dest = Mapper.Map<Source1, Destination1>(source);
return View(dest);
}
□ 要点
派生ValueResolver<,>实现自定义解析器,实现对源属性的"计算" 转换成目标属性。
源中的复杂属性和Get...方法转换成目标属性
□ Domain model
public class Order2
{
private readonly IList<OrderLineItem2> _orderLineItems = new List<OrderLineItem2>();
public Customer2 Customer { get; set; }
public OrderLineItem2[] GetOrderlineItems()
{
return _orderLineItems.ToArray();
}
public void AddOrderLineItem(Product2 product, int quantity)
{
_orderLineItems.Add(new OrderLineItem2(product, quantity));
}
public decimal GetTotal()
{
return _orderLineItems.Sum(li => li.GetTotal());
}
}
public class OrderLineItem2
{
public OrderLineItem2(Product2 product, int quantity)
{
Product = product;
Quantity = quantity;
}
public Product2 Product { get; set; }
public int Quantity { get; set; }
public decimal GetTotal()
{
return Quantity*Product.Price;
}
}
public class Customer2
{
public string Name { get; set; }
}
public class Product2
{
public string Name { get; set; }
public decimal Price { get; set; }
}
□ View model
public class Order2Dto
{
public string CustomerName { get; set; }
public decimal Total { get; set; }
}
□ 映射配置
Mapper.CreateMap<Order2, Order2Dto>();
□ 使用
public ActionResult Complex()
{
var customer = new Customer2()
{
Name = "Darren"
};
var order = new Order2()
{
Customer = customer
};
var product = new Product2()
{
Name = "bosco",
Price = 5.00m
};
order.AddOrderLineItem(product, 10);
Order2Dto dto = Mapper.Map<Order2, Order2Dto>(order);
return View(dto);
}
□ 要点
目标中的属性遵循惯例:
○ 源中复杂属性名+复杂属性对应类的属性,构成了目标属性
○ 源中Get...()方法转换成目标中的...属性