关于AutoMapper的接纳最终只是简单写了下, //属性匹配

写在眼下

  AutoMapper目录:

  本篇目录:

  上一篇《【道德经】漫谈实体、对象、DTO及AutoMapper的施用 》,因为内容写的有点跑偏,关于AutoMapper的利用最后只是简单写了下,很掌握这种回顾的接纳情势不能够满意项目中复杂的须求,网上找了下AutoMapper相关文书档案,但基本上都是像自家一样不难的概述下,看来懒的不只有本身贰个,哈哈。在AutoMapper
官方文书档案
中找到其选择的详细表达,天书一样的英文,然后就找有关汉语文书档案,最终如故没找到,这边无法,只可以自身动手,丰衣足食了。克罗地亚(Croatia)语牛逼的能够直接略过,查看英文文书档案,本篇也不算是翻译,因为小编拉脱维亚语实在拿不入手,只是依照示例加上自个儿的一对理解,做个上学笔记,不对的地方还请指正。

  注:尽管上一篇写跑偏了,可是自身真的很喜欢道德经,除了为人处世,在软件设计那方面实际也有反映,也意在得以应用到那上头,假如你和本身有同一的想法,请在点击布告栏中的QQ链接,知音难觅啊!

AutoMapper的匹配

     1,智能匹配

   
 AutoMapper能够自动识别和包容超过6/10目的属性:    

    • 假定源类和目的类的习性名称一致,直接匹配,不区分轻重缓急写

    • 指标项目标CustomerName能够匹配源类型的Customer.Name

    • 对象项指标Total能够匹配源类型的GetTotal()方法

    2,自定义匹配

    Mapper.CreateMap<Calendar伊夫nt, Calendar伊夫ntForm>()      
                                           
 //属性匹配,匹配源类中Work伊夫nt.Date到伊芙ntDate

    .ForMember(dest => dest.EventDate, opt => opt.MapFrom(src
=> src.WorkEvent.Date))

    .ForMember(dest => dest.SomeValue, opt => opt.Ignore())  
                                              //忽略指标类中的属性

    .ForMember(dest => dest.TotalAmount, opt =>
opt.MapFrom(src => src.TotalAmount ?? 0))  //复杂的分外

    .ForMember(dest => dest.OrderDate, opt =>
opt.UserValue<DateTime>(DateTime.Now));      //固定值匹配

Flattening-复杂到回顾

  Flattening
翻译为压扁、拉平、扁平化的情趣,可以领略为使原本复杂的布局变得简化,大家先看下领域模型和DTO代码:

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

  可以看看世界模型 Order
是很复杂的,可是对于工作场景中的OrderDto却很简短,唯有CustomerName和Total五个属性,AutoMapper配置代码:

 1         public void Example()
 2         {
 3             var customer = new Customer
 4             {
 5                 Name = "George Costanza"
 6             };
 7             var order = new Order
 8             {
 9                 Customer = customer
10             };
11             var bosco = new Product
12             {
13                 Name = "Bosco",
14                 Price = 4.99m
15             };
16             order.AddOrderLineItem(bosco, 15);
17             // 配置 AutoMapper
18             Mapper.CreateMap<Order, OrderDto>();
19             // 执行 mapping
20             OrderDto dto = Mapper.Map<Order, OrderDto>(order);
21             Console.WriteLine("CustomerName:" + dto.CustomerName);
22             Console.WriteLine("Total:" + dto.Total);
23         }

  转换职能:

图片 1

  可以看看配置极度的简要,只要设置下Order和OrderDto之间的体系映射就可以了,大家看OrderDto中的CustomerName和Total属性在天地模型Order中并从未与之相对性,没什么能够变换呢,感觉好神奇的样板,其实仔细发现那么些属性的命名都有一定的平整,AutoMapper在做分析的时候会鲁人持竿帕斯CarlCase(帕斯卡命名法),便是一种变量命名法,除了PascalCase还有Hungarian(匈牙利(Hungary)命名法)和camelCase(骆驼命名法),帕斯CarlCase正是指混合使用大小写字母来组合变量和函数的名字,首字母要大写,camelCase首字母小写,大家C#取名中,一般采用的是camelCase和帕斯CarlCase,比较高级的是帕斯CarlCase。

  可是为啥AutoMapper会解析Total呢?因为在天地模型Order中有个GetTotal()方法,AutoMapper会解析“Get”之后的单词,所以会与Total相对应,假设您把OrderDto的属性“Total”改为“Totals”,就会意识赢得的“Totals”为0。驾驭了AutoMapper的解析方法,大家就要留心在编辑变量、属性或是艺术名称的时候势供给规范,那也是一种好的习惯。

直白匹配

源类的天性名称和目的类的性情名称一致(不区分轻重缓急写),直接匹配,Mapper.CreateMap<source,dest>();无需做其余处理,此处不再细述

Projection-不难到复杂

  Projection
翻译为影子,Flattening是由复杂结构简化,Projection正好相反,投影能够清楚为由原始结构云谲风诡,大家看下二种转移结构:

 1     public class CalendarEvent
 2     {
 3         public DateTime EventDate { get; set; }
 4         public string Title { get; set; }
 5     }
 6 
 7     public class CalendarEventForm
 8     {
 9         public DateTime EventDate { get; set; }
10         public int EventHour { get; set; }
11         public int EventMinute { get; set; }
12         public string Title { get; set; }
13     }

  Calendar伊夫nt是原本结构,Calendar伊夫ntForm是大家必要更换后的构造,能够看到Calendar伊芙ntForm要比Calendar伊芙nt结构复杂些,看下AutoMapper配置转换代码:

 1         public void Example()
 2         {
 3             var calendarEvent = new CalendarEvent
 4             {
 5                 EventDate = new DateTime(2008, 12, 15, 20, 30, 0),
 6                 Title = "Company Holiday Party"
 7             };
 8 
 9             // 配置 AutoMapper
10             Mapper.CreateMap<CalendarEvent, CalendarEventForm>()
11                 .ForMember(dest => dest.EventDate, opt => opt.MapFrom(src => src.EventDate.Date))//定义映射规则
12                 .ForMember(dest => dest.EventHour, opt => opt.MapFrom(src => src.EventDate.Hour))//定义映射规则
13                 .ForMember(dest => dest.EventMinute, opt => opt.MapFrom(src => src.EventDate.Minute));//定义映射规则
14 
15             // 执行 mapping
16             CalendarEventForm form = Mapper.Map<CalendarEvent, CalendarEventForm>(calendarEvent);
17 
18             Console.WriteLine("EventDate:"+form.EventDate);
19             Console.WriteLine("EventHour:" + form.EventHour);
20             Console.WriteLine("EventMinute:" + form.EventMinute);
21             Console.WriteLine("Title:" + form.Title);
22         }

  和Flattening不相同的是,我们除了定义类型映射,还要自定义映射规则,src.伊芙ntDate.Date指向dest.伊夫ntDate,src.伊夫ntDate.Minute指向dest.伊夫ntMinute,src.伊芙ntDate.Hour指向dest.伊夫ntHour,当然我们还能在MapFrom方法中做一些良莠不齐的投射关系操作,MapFrom接受1个lambda表明式作为参数,能够是其余的Func表明式。Projection适用于由不难到复杂的协会映射,一般彰显在事情场景很复杂的情事下。

  【勘误:Projection也不必然适用在由简单到复杂的场合,应该说选用Projection就是把AutoMapper的映射配置交给用户来操作】

Flattening

 将三个复杂的对象模型拉伸为,大概扁平化为多个简约的目的模型,如上边这几个复杂的靶子模型:

        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 Product
        {
            public decimal Price { get; set; }
            public string Name { get; set; }
        }

        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; }
        }

大家要把这一繁杂的对象简化为OrderDTO,只含有某一场景所急需的数码:

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

运用AutoMapper转换:

            public void Example()
            {
                // Complex model
                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, 15);

                // Configure AutoMapper
                var config = new MapperConfiguration(cfg => cfg.CreateMap<Order, OrderDto>());

                // Perform mapping
                var mapper = config.CreateMapper();
                OrderDto dto = mapper.Map<Order, OrderDto>(order);

                dto.CustomerName.ShouldEqual("George Costanza");
                dto.Total.ShouldEqual(74.85m);
            }

能够见到只要设置下Order和OrderDto之间的档次映射就能够了,我们看OrderDto中的CustomerName和Total属性在世界模型Order中并不曾与之相对性,AutoMapper在做分析的时候会依据帕斯CarlCase(帕斯卡命名法),CustomerName其实是由Customer+Name
得来的,是AutoMapper的一种炫耀规则;而Total是因为在Order中有GetTotal()方法,AutoMapper会解析“Get”之后的单词,所以会与Total对应。在编写代码进度中得以应用这种规则来定义名称达成活动转换。

Configuration Validation-配置验证

  大家在利用Flattening的前提是大家必要转移的布局命名是从未不当的,可是假使大家从没运用帕斯CarlCase命名法,可能说大家命名是荒唐的,该如何做呢?比如下边代码:

1         public class Source
2         {
3             public int SomeValue { get; set; }
4         }
5 
6         public class Destination
7         {
8             public int SomeValuefff { get; set; }
9         }

  能够看来Source和Destination中的字段并不相对应,大家测试下AutoMapper映射:

图片 2

  AssertConfigurationIsValid方法是认证结构映射的,要是安插不正确,会报“AutoMapperConfigurationException”极度错误,怎么样消除这几个难点?你恐怕会说,就无法改下SomeValuefff的称号吗?那种艺术能够,但是只要工作场景中必须求采用如何是好吧,看了地点Projection的照射配置,你大概想到消除方法了,如下:

1             Mapper.CreateMap<Source, Destination>()
2                 .ForMember(dest => dest.SomeValuefff, opt => opt.MapFrom(src => src.SomeValue));

  名称不对,大家得以自定义映射规则,纵然那种形式可以,不过若是事情场景中SomeValuefff并不供给,那我们改如何做?既然有标题,就有化解之道,AutoMapper提供了Ignore方法,忽略不要求映射的数据结构,大家那样安顿就足以了:

1             Mapper.CreateMap<Source, Destination>()
2                 .ForMember(dest => dest.SomeValuefff, opt => opt.Ignore());

Projection

Projection能够掌握为与Flattening相反,Projection是将源对象映射到三个不完全与源对象匹配的目的对象,须求制订自定义成员,如上边包车型大巴源对象:

        public class CalendarEvent
        {
            public DateTime EventDate { get; set; }
            public string Title { get; set; }
        }

对象对象:

        public class CalendarEventForm
        {
            public DateTime EventDate { get; set; }
            public int EventHour { get; set; }
            public int EventMinute { get; set; }
            public string Title { get; set; }
        }

AutoMapper配置转换代码:

            public void Example()
            {
                // Model
                var calendarEvent = new CalendarEvent
                    {
                        EventDate = new DateTime(2008, 12, 15, 20, 30, 0),
                        Title = "Company Holiday Party"
                    };

                var config = new MapperConfiguration(cfg =>
                {
                    // Configure AutoMapper
                    cfg.CreateMap<CalendarEvent, CalendarEventForm>()
                        .ForMember(dest => dest.EventDate, opt => opt.MapFrom(src => src.EventDate.Date))
                        .ForMember(dest => dest.EventHour, opt => opt.MapFrom(src => src.EventDate.Hour))
                        .ForMember(dest => dest.EventMinute, opt => opt.MapFrom(src => src.EventDate.Minute));
                });

                // Perform mapping
                var mapper = config.CreateMapper();
                CalendarEventForm form = mapper.Map<CalendarEvent, CalendarEventForm>(calendarEvent);

                form.EventDate.ShouldEqual(new DateTime(2008, 12, 15));
                form.EventHour.ShouldEqual(20);
                form.EventMinute.ShouldEqual(30);
                form.Title.ShouldEqual("Company Holiday Party");
            }

Lists and Array-集合和数组

  有时候大家除了项目映射之外,还索要对聚集类型实行映射,先看个示范:

 1             public void Example()
 2             {
 3                 var sources = new[]
 4                     {
 5                         new Source {Value = 5},
 6                         new Source {Value = 6},
 7                         new Source {Value = 7}
 8                     };
 9                 //配置AutoMapper
10                 Mapper.Initialize(cfg =>
11                 {
12                     cfg.CreateMap<Source, Destination>();
13                 });
14                 //配置和执行映射
15                 IEnumerable<Destination> ienumerableDest = Mapper.Map<Source[], IEnumerable<Destination>>(sources);
16                 ICollection<Destination> icollectionDest = Mapper.Map<Source[], ICollection<Destination>>(sources);
17                 IList<Destination> ilistDest = Mapper.Map<Source[], IList<Destination>>(sources);
18                 List<Destination> listDest = Mapper.Map<Source[], List<Destination>>(sources);
19 
20                 Console.WriteLine("ienumerableDest.Count:" + ienumerableDest.Count());
21                 Console.WriteLine("icollectionDest.Count:" + icollectionDest.Count());
22                 Console.WriteLine("ilistDest.Count:" + ilistDest.Count());
23                 Console.WriteLine("listDest.Count:" + listDest.Count());
24             }

  转换结果:

图片 3

  Source和Destination结构类型唯有3个Value属性,可以观望对集合类型映射也很不难,只要求履行Mapper.Map泛型方法,钦命须求转移的集合类型即可,AutoMapper所支持的聚合类型包含:

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

  我们在动用Mapper.Map执行项目映射的时候,假如来源类型帮衬上述集合类型,大家能够把来自类型省略掉,因为AutoMapper会自动判断传入对象sources的品类,如下:

1                 IEnumerable<Destination> ienumerableDest = Mapper.Map<IEnumerable<Destination>>(sources);
2                 ICollection<Destination> icollectionDest = Mapper.Map<ICollection<Destination>>(sources);
3                 IList<Destination> ilistDest = Mapper.Map<IList<Destination>>(sources);
4                 List<Destination> listDest = Mapper.Map<List<Destination>>(sources);

  还有一种情状是,在动用集合类型类型的时候,类型之间存在继续关系,例如下边大家供给更换的档次:

 1             public class ParentSource
 2             {
 3                 public int Value1 { get; set; }
 4             }
 5             public class ChildSource : ParentSource
 6             {
 7                 public int Value2 { get; set; }
 8             }
 9             public class ParentDestination
10             {
11                 public int Value1 { get; set; }
12             }
13             public class ChildDestination : ParentDestination
14             {
15                 public int Value2 { get; set; }
16             }

  ChildSource继承ParentSource,ChildDestination继承ParentDestination,看下AutoMapper配置转换代码:

 1             public void Example()
 2             {
 3                 var sources = new[]
 4                     {
 5                         new ParentSource(),
 6                         new ChildSource(),
 7                         new ParentSource()
 8                     };
 9                 //配置AutoMapper
10                 Mapper.Initialize(cfg =>
11                 {
12                     cfg.CreateMap<ParentSource, ParentDestination>()
13                         .Include<ChildSource, ChildDestination>();
14                     cfg.CreateMap<ChildSource, ChildDestination>();
15                 });
16                 //配置和执行映射
17                 var destinations = Mapper.Map<ParentSource[], ParentDestination[]>(sources);
18                 Console.WriteLine("destinations[0] Type:" + destinations[0].GetType().ToString());
19                 Console.WriteLine("destinations[1] Type:" + destinations[1].GetType().ToString());
20                 Console.WriteLine("destinations[2] Type:" + destinations[2].GetType().ToString());
21             }

  转换结果:

图片 4

  注意在Initialize起先化CreateMap举行项目映射配置的时候有个Include泛型方法,签名为:“Include
this configuration in derived types’
maps”,大概意思为涵盖派生类型中配置,ChildSource是ParentSource的派生类,ChildDestination是ParentDestination的派生类,cfg.CreateMap<ParentSource,
ParentDestination>().Include<ChildSource, ChildDestination>();
那段代码只是表明ParentSource和ChildSource之间存在的涉嫌,大家只要把这段代码注释掉,就会报上边“AutoMapperMappingException”类型钦赐不科学的十三分错误,假设我们把下边那段代码:“cfg.CreateMap<ChildSource,
ChildDestination>();”注释掉,转换结果为:

图片 5

  即便并未报“AutoMapperMappingException”很是,不过能够看出AutoMapper并不曾从ChildSource类型映射到ChildDestination类型,而是自行映射到基类型,上边那段映射代码只是表明派生类和基类之间存在的涉嫌,假设派生类要求映射的话,是亟需添加派生类的映射的。

 Configuration Validation

在展开对象映射的时候,有也许会并发属性名称或然自定义匹配规则不正确而又没有意识的景观,在程序执行时就报错,由此,AutoMapper提供的AssertConfigurationIsValid()方法来证实结构映射是或不是正确。

 config.AssertConfigurationIsValid();假诺映射错误,会报“AutoMapperConfigurationException”非常错误,就足以拓展调剂修改了

Nested mappings-嵌套映射

  大家地点说的集中映射格局都以简约类型映射,就是连串中并不包罗其余类别的映照,怎么着在嵌套类型中推行映射?请看上边示例:

 1             public class OuterSource
 2             {
 3                 public int Value { get; set; }
 4                 public InnerSource Inner { get; set; }
 5             }
 6             public class InnerSource
 7             {
 8                 public int OtherValue { get; set; }
 9             }
10             public class OuterDest
11             {
12                 public int Value { get; set; }
13                 public InnerDest Inner { get; set; }
14             }
15             public class InnerDest
16             {
17                 public int OtherValue { get; set; }
18             }

  OuterSource和OuterDest类型是我们要求映射的类型,能够观察OuterSource类型中嵌套了InnerSource类型,OuterDest类型中嵌套了InnerDest类型,AutoMapper类型映射配置代码:

 1             public void Example()
 2             {
 3                 var source = new OuterSource
 4                 {
 5                     Value = 5,
 6                     Inner = new InnerSource { OtherValue = 15 }
 7                 };
 8                 //配置AutoMapper
 9                 Mapper.CreateMap<OuterSource, OuterDest>();
10                 Mapper.CreateMap<InnerSource, InnerDest>();
11                 //验证类型映射是否正确
12                 Mapper.AssertConfigurationIsValid();
13                 //执行映射
14                 var dest = Mapper.Map<OuterSource, OuterDest>(source);
15                 Console.WriteLine("dest.Value:" + dest.Value);
16                 Console.WriteLine("dest.Inner is null:" + (dest.Inner == null ? "true" : "false"));
17                 Console.WriteLine("dest.Inner.OtherValue:" + dest.Inner.OtherValue);
18             }

  转换结果:

图片 6

  上边代码中能够看来,对于嵌套映射,大家不必要配置如何,只要内定下类型映射关系和嵌套类型映射关系就足以了,也便是那段代码:“Mapper.CreateMap<InnerSource,
InnerDest>();”
其实大家在证实类型映射的时候拉长Mapper.AssertConfigurationIsValid();
那段代码看是否抛出“AutoMapperMappingException”相当来判定项目映射是或不是正确,因为AssertConfigurationIsValid方法没有重回值,只辛亏catch中抓获了,个人感觉AutoMapper能够提供个bool类型的重回值,验证成功则赶回true。

 Lists and Array

AutoMapper补助的源集合类型包含:

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

有一种情形是,在应用集合类型类型的时候,类型之间存在继续关系,例如下边我们必要更换的门类:

            //源对象
            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需求孩子映射的显式配置,AutoMapper配置转换代码:

                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap<ParentSource, ParentDestination>()
                        .Include<ChildSource, ChildDestination>();
                    cfg.CreateMap<ChildSource, ChildDestination>();
                });

                var sources = new[]
                    {
                        new ParentSource(),
                        new ChildSource(),
                        new ParentSource()
                    };

                var destinations = config.CreateMapper().Map<ParentSource[], ParentDestination[]>(sources);

                destinations[0].ShouldBeType<ParentDestination>();
                destinations[1].ShouldBeType<ChildDestination>();
                destinations[2].ShouldBeType<ParentDestination>();

瞩目在Initialize开端化CreateMap实行项目映射配置的时候有个Include泛型方法,签名为:“Include
this configuration in derived types’
maps”,大致意思为带有派生类型中安插,ChildSource是ParentSource的派生类,ChildDestination是ParentDestination的派生类,cfg.CreateMap<ParentSource,
ParentDestination>().Include<ChildSource, ChildDestination>();
那段代码是表达ParentSource和ChildSource之间存在的涉嫌,并且要要展现的陈设。

后记

  示例代码下载:http://pan.baidu.com/s/10A7WM

  贪多嚼不烂,关于AutoMapper的行使先收拾那几个,前面会陆续更新,还请关心。

  AutoMapper在布置项目映射最注指标有些是,类型中的名称一定要依照帕斯CarlCase命名规则(Projection和Ignore除外)。

  假诺你以为本篇作品对您抱有帮助,请点击右下部“推荐”,^_^

  参考资料:

 Nested mappings

嵌套对象映射,例如下边包车型地铁目的:

       public class OuterSource
            {
                public int Value { get; set; }
                public InnerSource Inner { get; set; }
            }

            public class InnerSource
            {
                public int OtherValue { get; set; }
            }

            //目标对象
            public class OuterDest
            {
                public int Value { get; set; }
                public InnerDest Inner { get; set; }
            }

            public class InnerDest
            {
                public int OtherValue { get; set; }
            }

AutoMapper配置转换代码:

                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap<OuterSource, OuterDest>();
                    cfg.CreateMap<InnerSource, InnerDest>();
                });
                config.AssertConfigurationIsValid();

                var source = new OuterSource
                    {
                        Value = 5,
                        Inner = new InnerSource {OtherValue = 15}
                    };

                var dest = config.CreateMapper().Map<OuterSource, OuterDest>(source);

                dest.Value.ShouldEqual(5);
                dest.Inner.ShouldNotBeNull();
                dest.Inner.OtherValue.ShouldEqual(15);

对此嵌套映射,只要钦命下类型映射关系和嵌套类型映射关系就足以了,也便是那段代码:“Mapper.CreateMap<InnerSource,
InnerDest>();”
其实大家在表明类型映射的时候添加Mapper.AssertConfigurationIsValid();
那段代码看是否抛出“AutoMapperMappingException”至极来判断项目映射是或不是科学。

参考资料

有关AutoMapper,陆续更新中…