代码改变世界

AutoMapper的简单使用

2015-07-21 11:32  老厨子  阅读(307)  评论(0编辑  收藏  举报

一、安装生成AutoMapper

在NuGget程序控制台输入指令:PM> install-package automapper,回车键确认安装AutoMapper

 

二、代码

安装完AutoMapper,添加引用

1. 代码模块

 首先要有完成转换的两个对象(我这里用NetBook,TextBook):

  (1)NetBook对象是用来接收数据的对象,就是接收数据库数据的完整对象

 1  public class NetBook {
 2         public int NId { get; set; }
 3         public string Use { get; set; }
 4         public string Content { get; set; }
 5         public string Property { get; set; }
 6         public int Total { get; set; }
 7         public string BackGroud { get; set; }
 8         public string User { get; set; }
 9         public string UserInfo { get; set; }
10         public bool IsDel { get; set; }
11         public DateTime Date { get; set; }
12     }

   (2)TextBook对象是用来显示出输的对象,减少数据的冗余和不必要显示的数据

1  public class TextBook{
2         public int TId { get; set; }
3         public string Use { get; set; }
4         public string ContentText { get; set; }
5         public string Property { get; set; }
6         public int PageTotal { get; set; }
7         public string BackGroud { get; set; }
8         public string User { get; set; }
9     }

   (3)代码主体部分

 1 StringBuilder str = new StringBuilder(200);
 2 
 3             //---数据源---
 4             NetBook netbook = new NetBook() {
 5                 BackGroud="蓝色",
 6                 Content="This Mylife",
 7                 Property="私人", 
 8                 Use="日志",
 9                 Date=DateTime.Now, 
10                 IsDel=false, 
11                 NId=1, 
12                 Total=int.MaxValue,
13                 User = "Coder",
14                 UserInfo="代码是生活的一部分"
15             };
16 
17             //---创建映射关系---
18             //--- 一一映射---
19             Mapper.CreateMap<NetBook, TextBook>()
20                 .ForMember(d => d.ContentText, dto => dto.MapFrom(c => c.Content))
21                 .ForMember(d => d.Use, dto => dto.MapFrom(c => c.Use))
22                 .ForMember(d => d.PageTotal, dto => dto.MapFrom(c => c.Total))
23                 .ForMember(d => d.Property, opt => opt.Ignore());//不映射此属性
24 
25             //---生成新对象---
26             TextBook textbook = Mapper.Map<NetBook, TextBook>(netbook);
27 
28             //---显示---
29             str.Append("<br />ID:"+textbook.TId);
30             str.Append("<br />如何使用:"+textbook.Use);
31             str.Append("<br />内容:"+textbook.ContentText);
32             str.Append("<br />总页数:"+textbook.PageTotal);
33             str.Append("<br />封面:"+textbook.BackGroud);
34             str.Append("<br />属性:"+textbook.Property);
35             str.Append("<br />使用者:" + textbook.User);
36             Response.Write(str.ToString());
View Code

   (4)出输结果

        ID:0
       如何使用:日志
       内容:This Mylife
       总页数:2147483647
       封面:蓝色
       属性:
       使用者:Coder

    (5)分析

          1. 在上面中没有实现Tid的映射,结果显示为0(即为int类型默认值)

          2.  .ForMember(d => d.Use, dto => dto.MapFrom(c => c.Use))实现了两个属性名相同的转换,出输结果相同,映射成功

          3. 没有对BackGroud属性创建映射关系,但是结果却是出输相同

          4.  .ForMember(d => d.PageTotal, dto => dto.MapFrom(c => c.Total))不同属性名之间的映射,结果出输相等,映射成功

          5. 两个对象的Property属性名相同,但是结果出输为Null

三、总结

    1. 对象属性名一致,可以不用创建映射关系就可以实现转换

    2. 实现属性名不一致的映射,其映射关系为:.ForMember(d => d.PageTotal(新数据对象属性), dto => dto.MapFrom(c => c.Total(源对象属性))),相同属性名也是这样的

    3. 不想实现相同属性名之间的映射,其映射关系为: .ForMember(d => d.Property(新数据对象属性), opt => opt.Ignore()(不实现映射关系))

 

 

 

PS:以上只是本人的学习总结,如果大家有更好的总结,更新的知识和技术,请提供分享啊!