AutoMapper(六)

简介:

List和数组

AutoMapper只要求元素类型的配置而不要求可能会用到的任何数组或者list类型。比如,我们有一个简单的源和目标类型:

public class Source{    public int Value { get; set; }}public class Destination{    public int Value { get; set; }}

支持所有的基本泛型集合,代码如下:

复制代码
class Program{    static void Main(string[] args)    {        Mapper.CreateMap<Source, Destination>();        var sources = new[]        {            new Source() {Value = 1},            new Source() {Value = 2},            new Source() {Value = 3},        };        IEnumerable<Destination> iEnumerableDests= Mapper.Map<IEnumerable<Destination>>(sources);        ICollection<Destination> iCollectionDests= Mapper.Map<ICollection<Destination>>(sources);        IList<Destination> iListDests= Mapper.Map<IList<Destination>>(sources);        List<Destination> listDests= Mapper.Map<List<Destination>>(sources);        Destination[] destsArr= Mapper.Map<Destination[]>(sources);        //这里只举两个例子,其他集合同理        foreach (var dest in iCollectionDests)        {            Console.Write(dest.Value+",");        }        Console.WriteLine();        foreach (var dest in destsArr)        {            Console.Write(dest.Value + ",");        }        Console.Read();    }}
复制代码

以上代码是集合和集合之间的映射,但是映射的配置CreateMap方法中只是配置的是类型之间的映射,而没有设计任何集合类型。

测试结果如下,可见集合之间映射成功:

image

具体来说,支持的源集合类型包括:

  • IEnumerable
  • IEnumerable<T>
  • ICollection
  • ICollection<T>
  • IList
  • IList<T>
  • List<T>
  • Arrays

集合中的多态元素类型

很多时候,在我们的源和目标类型中可能有一个类型层次关系。AutoMapper支持多态数组和集合,因此如果发现派生的源或者目标类型,就会使用它们。

public class ParentSource{    public int Value1 { get; set; }}public class ChildSource : ParentSource{    public int Value2 { get; set; }}public class ParentDestination{    public int Value1 { get; set; }}public class ChildDestination : ParentDestination{    public int Value2 { get; set; }}

AutoMapper仍然要求显示配置孩子映射,因为它不能“猜出”具体使用哪一个孩子目标映射。

在Main方法中添加如下代码:

Mapper.Initialize(c =>{    c.CreateMap<ParentSource, ParentDestination>()        .Include<ChildSource, ChildDestination>();    c.CreateMap<ChildSource, ChildDestination>();});var sources = new[]{    new ParentSource(){Value1 = 11},    new ChildSource(){Value2 = 22},    new ParentSource(),};var dests = Mapper.Map<ParentDestination[]>(sources);Console.WriteLine(dests[0]);Console.WriteLine(dests[1]);Console.WriteLine(dests[2]);

测试结果如下:

image

上面我们创建了一个源的数组,其中包含两个ParentSource和一个ChildSource,所以两个ParentSource成功地映射到了ParentDestination,而CreateMap配置中,ParentSource到ParentDestination的映射配置包含了ChildSource到ChildDestination的配置,所以执行Mapper.Map<ParentDestination[]>(sources)的时候,也可以将ChildSource映射到ChildDestination。

映射继承

在派生类中标明继承

上面的代码,是在基类中配置继承的,除此之外,也可以在派生类中配置继承:

//在基类中配置继承 Mapper.Initialize(c => {     c.CreateMap<ParentSource, ParentDestination>()         .Include<ChildSource, ChildDestination>();     c.CreateMap<ChildSource, ChildDestination>(); }); //在派生类中配置继承 Mapper.Initialize(c => {     c.CreateMap<ParentSource, ParentDestination>();     c.CreateMap<ChildSource, ChildDestination>()         .IncludeBase<ParentSource, ParentDestination>(); });

继承映射属性

这里介绍一下额外的复杂性,因为一个属性映射时可以有多种方式。下面是这些源的优先级:

  • 显式映射 (使用.MapFrom())
  • 继承的显式映射 
  • 惯例映射 (通过惯例匹配的属性)
  • 忽略的属性映射

下面来演示一下:

这里还是用上面定义的四个类:Order,OrderDto,PCOrder,MobileOrder:

//领域对象public class Order { }//电脑端订单public class PCOrder : Order{    public string Referrer { get; set; }}//手机订单public class MobileOrder : Order { }//Dtospublic class OrderDto{    public string Referrer { get; set; }}

配置映射的方法使用的是在父类中配置继承映射

//在父类中配置继承映射Mapper.CreateMap<Order, OrderDto>()    .Include<PCOrder,OrderDto>()    .Include<MobileOrder,OrderDto>()    .ForMember(o => o.Referrer, m => m.Ignore());//这里配置了忽略目标属性Referrer的映射Mapper.CreateMap<PCOrder,OrderDto>();Mapper.CreateMap<MobileOrder, OrderDto>();
// 执行映射var order = new PCOrder() { Referrer = "天猫" };var mapped = Mapper.Map<OrderDto>(order);Console.WriteLine(mapped.Referrer);

执行结果如下:

image

注意在我们的映射配置中,我们已经忽略了Referrer(因为Order基类中不存在这个属性),但是在基类的映射中,惯例比忽略的属性有更高的优先级,因而属性仍然得到了映射。





本文转自tkbSimplest博客园博客,原文链接:http://www.cnblogs.com/farb/p/4944018.html,如需转载请自行联系原作者

目录
相关文章
|
前端开发
8分钟学会使用AutoMapper
原文:8分钟学会使用AutoMapper 一.什么是AutoMapper与为什么用它。 它是一种对象与对象之间的映射器,让AutoMapper有意思的就是在于它提供了一些将类型A映射到类型B这种无聊的实例,只要B遵循AutoMapper已经建立的惯例,那么大多数情况下就可以进行相互映射了。
1037 0
AutoMapper 使用总结1
原文:AutoMapper 使用总结1 初识AutoMapper 在开始本篇文章之前,先来思考一个问题:一个项目分多层架构,如显示层、业务逻辑层、服务层、数据访问层。层与层访问需要数据载体,也就是类。
868 0
|
测试技术 容器