1.嵌套映射

 1 namespace Second
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             Mapper.CreateMap<OuterSource, OuterDest>();
 8             Mapper.CreateMap<InnerSource, InnerDest>();
 9             Mapper.AssertConfigurationIsValid();
10             var source = new OuterSource
11             {
12                 Value = 5,
13                 Inner = new InnerSource { OtherValue=15 }
14             };
15             var dest = Mapper.Map<OuterSource, OuterDest>(source);
16         }
17     }
18
19     public class OuterDest
20     {
21         public int Value { get; set; }
22         public InnerDest Inner { get; set; }
23     }
24
25     public class InnerDest
26     {
27         public int OtherValue { get; set; }
28     }
29
30     public class OuterSource
31     {
32         public int Value { get; set; }
33         public InnerSource Inner { get; set; }
34     }
35     public class InnerSource
36     {
37         public int OtherValue { get; set; }
38     }
39 }

View Code

2.自定义类型转换

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Mapper.CreateMap<string, int>().ConvertUsing(Convert.ToInt32);
 6             Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());
 7           //  Mapper.CreateMap<string, DateTime>().ConvertUsing(Convert.ToDateTime);
 8             Mapper.CreateMap<string, Type>().ConvertUsing<TypeTypeConverter>();
 9             Mapper.CreateMap<Source, Destination>();
10             Mapper.AssertConfigurationIsValid();
11             var source = new Source
12             {
13                 Value1 = "5",
14                 Value2 = "01/01/2000",
15                 Value3 = "AutoMapperSamples.GlobalTypeConverters.GlobalTypeConverters+Destination"
16             };
17            var result= Mapper.Map<Source, Destination>(source);
18
19         }
20     }
21     public class DateTimeTypeConverter : ITypeConverter<string,DateTime>
22     {
23         public DateTime Convert(ResolutionContext context)
24         {
25             return System.Convert.ToDateTime(context.SourceValue);
26         }
27     }
28     public class TypeTypeConverter : ITypeConverter<string, Type>
29     {
30         public Type Convert(ResolutionContext context)
31         {
32             return context.SourceType;
33         }
34     }
35
36
37     public class Source
38     {
39         public string Value1 { get; set; }
40         public string Value2 { get; set; }
41         public string Value3 { get; set; }
42     }
43     public class Destination
44     {
45         public int Value1 { get; set; }
46         public DateTime Value2 { get; set; }
47         public Type Value3 { get; set; }
48     }

View Code

3.自定义值转换

 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             //Mapper.CreateMap<Source, Destination>()
 6             //    .ForMember(a => a.Total, b => b.ResolveUsing<CustomResolver>());
 7
 8             //不使用反射构造CustomResolver
 9             Mapper.CreateMap<Source, Destination>()
10                 .ForMember(opt => opt.Total,
11                 src => src.ResolveUsing<CustomResolver>()
12                 .ConstructedBy(()=>new CustomResolver()));
13
14             var source = new Source
15             {
16                 Value1 = 5,
17                 Value2 = 7
18             };
19
20             var result = Mapper.Map<Source, Destination>(source);
21
22         }
23     }
24
25
26     public class CustomResolver : ValueResolver<Source, int>
27     {
28         protected override int ResolveCore(Source source)
29         {
30            return source.Value1 + source.Value2;
31         }
32     }
33
34
35     public class Source
36     {
37         public int Value1 { get; set; }
38         public int Value2 { get; set; }
39     }
40
41     public class Destination
42     {
43         public int Total { get; set; }
44     }

View Code

4.替代NULL

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Mapper.CreateMap<Source, Dest>()
 6                 .ForMember(dest => dest.Value, opt => opt.NullSubstitute("hi"));
 7             var source = new Source {  Value=null};
 8             var d = Mapper.Map<Source, Dest>(source);
 9
10             Console.WriteLine(d.Value);//hi
11         }
12     }
13
14     class Source
15     {
16         public string Value { get; set; }
17     }
18
19     class Dest
20     {
21         public string Value { get; set; }
22     }

View Code

5.在map之前或之后进行处理

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             //Mapper.CreateMap<Source, Dest>()
 6             //    .BeforeMap((src, dest) => src.Value = src.Value + "hh")
 7             //    .AfterMap((src,dest)=>dest.Value="glzsk");
 8
 9             Mapper.CreateMap<Source, Dest>();
10
11             var a = new Source { Value="A" };
12           //  var b = Mapper.Map<Dest>(a);
13             var b = Mapper.Map<Source, Dest>(a, opt =>
14                 {
15                     opt.AfterMap((src, dest) => dest.Value = "glzsk");
16                     opt.BeforeMap((src, dest) => src.Value = src.Value + "hh");
17                 }
18                );
19             Console.WriteLine(a.Value+"\r\n"+b.Value);
20             Console.ReadLine();
21         }
22     }
23
24     class Source
25     {
26         public string Value { get; set; }
27     }
28
29     class Dest
30     {
31         public string Value { get; set; }
32     }

View Code

转载于:https://www.cnblogs.com/goodlucklzq/p/4643574.html

AutoMapper2相关推荐

  1. java automapper_实现AutoMapper(1.0版本)

    最近有个需求就是实体之间自动转换,网上肯定有很多现成的实现,不过还是自己写了一个,就当对java高级特性的一个熟悉的过程.这中间包含了泛型,反射,lamada表达式.对于想了解java高级特性的人来说 ...

  2. 【AutoMapper官方文档】DTO与Domin Model相互转换(下)

    Mapping Inheritance-映射继承 关于映射继承,其实在"Lists and Array-集合和数组"这一节点有提到,但是只是说明下AutoMapper解决映射继承所 ...

  3. 手动搭建ABP2.1.3 Zero——基础框架

    一.基础层搭建 二.PM.Core 三.PM.EntityFramework 四.PM.Application 五.PM.WebApi 六.PM.Web(MPA) 七.PM.Web(SPA) 八.单元 ...

最新文章

  1. 中间画一条短竖线_许愿孔明灯怎么画,简约好看的孔明灯简笔画教程
  2. [原创]Fluent NHibernate之旅
  3. 不要在facelets中重复表情
  4. BFS - 20190206
  5. 语音识别在智能交通中的几种应用分析
  6. 这100道Java面试题,面试不是什么难事了!
  7. NOI2019游记 —— 夏花般绚烂,繁星般璀璨
  8. C语言--冒泡排序法(详细注释)
  9. python部署阿里云_python部署到阿里云
  10. 华为eNSP静态路由原理与配置实例详解
  11. Python 列表与元组
  12. Ubuntu(21.04)下UHD(4.1)与Gnuradio安装配置--USRP X410软件无线电平台开发
  13. ico的尺寸_Favicon.ico浏览器图标文件制作和正确使用
  14. 在线学习Java的资源网站
  15. ColorMatrix 矩阵效果,即美图秀秀图片滤镜效果的思路
  16. Python——时间与时间戳之间的转换
  17. C++将二进制转换为十进制
  18. Appium(Python)测试混血App
  19. 最小值c语言编写自定义函数,C语言笔记55:自定义函数[老九学堂]
  20. Vue elementUI-select多选下拉框数据回显成功后,点击下拉选项或删除回显数据无反应...

热门文章

  1. 一次Linux磁盘损坏导致系统不可用恢复实例
  2. 你必须失败---来自迈克尔·乔丹的6条教训
  3. Hashtable排序
  4. Cannot resolve corresponding JNI function
  5. innodb Cardinality学习笔记
  6. .net 和 java 技术对应关系
  7. webpack 配置
  8. C#中Thread.IsBackground 属性
  9. samtools idxstats
  10. poj 1469 COURSES 解题报告