码迷,mamicode.com
首页 > 移动开发 > 详细

AutoMapper2

时间:2015-07-15 16:46:16      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

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

 

AutoMapper2

标签:

原文地址:http://www.cnblogs.com/goodlucklzq/p/4643574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!