本篇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...()方法轉換成目標中的...屬性