五步掌握OOM框架AutoMapper基本使用

所属分类: 网络编程 / ASP.NET 阅读数: 1749
收藏 0 赞 0 分享

写在前面

OOM顾名思义,Object-Object-Mapping实体间相互转换,AutoMapper也是个老生常谈了,其意义在于帮助你无需手动的转换简单而又麻烦的实体间关系,比如ViewModel和entity的转换,SearchModel和Entity的转换,我这篇分享的意义在于,网上大多数的分享都是几年前的,很多方法已经被废弃,到了编译器里会告诉你该方法已经过时,废弃的,不建议使用的,比如Mapper.CreateMap等方法,当然老司机大多数直接就去github看文档了,或者google一下就了解了,但是中文资料关于方法废弃后,并没有什么说明了。本篇的五个实例可以帮你解决常见的基本问题.

预备

首先我们预备一些ViewModel和TModel。ViewModel就是你和用户交互的实体。TModel就是你与数据库打交道的实体。

实体展示如下:

TModel有如下三个简单的实体,他们有独立的实体,也有一对多的实体。

public class TAddress
{
 public string Country { get; set; }
 public string City { get; set; }
 public string Street { get; set; }
 public string PostCode { get; set; }
 public string CreateTime { get; set; }
 public int CreateUserId { get; set; }
}
public class TAuthor
 {
  public string Name { get; set; }
  public string Description { get; set; }
  public List<TContactInfo> ContactInfo { get; set; }
 }
 public class TContactInfo
 {
 public int Id { get; set; }
 public string Email { get; set; }
 public string Blog { get; set; }
 public string Twitter { get; set; }
 }

ViewModel如下三个:

public class VM_Address
 {
 public string Country { get; set; }
 public string City { get; set; }
 public string City2 { get; set; }
 }
 public class VM_Author
 {
 public string Name { get; set; }
 public string Description { get; set; }
 public List<VM_ContactInfo> ContactInfo { get; set; }
 }
 public class VM_ContactInfo
 {
 public int Id { get; set; }
 public string Email { get; set; }
 public string Blog { get; set; }
 public string Twitter { get; set; }
 }

单个实体转换

单个实体转换的时候,在属性字段名称完全匹配的情况下,你只需指定两个实体间的转换规则,指定source源实体和destination目标实体。那么你应该参照如下实例:

VM_Address dto = new VM_Address
  {
  Country = "China",
  City = "Beijing"
  };
  Mapper.Initialize(m => m.CreateMap<VM_Address, TAddress>());
  TAddress address = Mapper.Map<VM_Address, TAddress>(dto);

请注意在AutoMapper5.x当中,Initialize来初始化你的规则是首选的。

在你指定转换规则后,请使用Map方法,进行转换并输出你的目标实体。还有第一个参数代表SourceModel,第二个参数是DestinationModel.

单个实体不同名属性转换

当你需要对不同名称的字段来进行映射的时候,请注意使用ForMember方法,第一个参数需要你制定所需特殊配置的目标字段,第二个参数你则需要制定你对该字段属性的操作,我选择了它提供的MapFrom方法,意义在于告诉AutoMapper,我需要讲目标实体的City来源 指定为 源实体的City2属性值。

VM_Address dto = new VM_Address
  {
  Country = "China",
  City2 = "Beijing"
  };
  Mapper.Initialize(m => m.CreateMap<VM_Address, TAddress>().ForMember(x => x.City, opt => opt.MapFrom(o => o.City2)));
  TAddress address = Mapper.Map<VM_Address, TAddress>(dto);

集合转换

在集合间转换的时候,你不需要配置目标List与源List对象中的匹配,而只需要配置你泛型对象的映射匹配关系。

  TAddress address = new TAddress { Country = "China", City = "Beijing" };
  TAddress address2 = new TAddress() { Country = "USA", City = "New York" };
  List<TAddress> addressList = new List<TAddress>() { address2, address };
  Mapper.Initialize(m => m.CreateMap<TAddress, VM_Address>());//这里仅需配置实体间的转换,而不是实体集合的转换
  List<VM_Address> res = Mapper.Map<List<TAddress>, List<VM_Address>>(addressList);

实体包含不同类型属性转换(忽略属性)

在实体包含不同类型属性的时候,比如TModel1中包含了一个List<TModel>,而你的ViewModel1中包含了一个List<ViewModel>.这个时候你可以选择忽略这个属性

 var contacts = new List<TContactInfo>() { new TContactInfo() 
          { Blog = "myblog", Email = "ws@qq.com" }, new TContactInfo() { Blog = "myblog", Email = "ll@qq.com" } };
  TAuthor author = new TAuthor() { Description = "描述", Name = "吴双", ContactInfo = contacts };
  Mapper.Initialize(m => { m.CreateMap<TAuthor, VM_Author>().ForMember(x => x.ContactInfo, opt => opt.Ignore()); });
       VM_Author dto = Mapper.Map<TAuthor, VM_Author>(author);
//这里的Ignore代表配置ContractInfo该属性的操作 为 忽略Ignore,映射时将忽略该属性 由于List<TContactInfo>()和List<VM_ContactInfo>() 是不同类型,所以需要配置忽略或者是特殊映射,特殊映射例子看下方

实体包含不同类型属性转换(指定属性Mapfrom)

当然你需要这个属性的时候,你可以不忽略他,而是使用MapFrom来进行特殊的指定,并且在类型不相同的时候,你要指定你两个类型间的映射匹配关系。正如下面实例中的

m.CreateMap<TContactInfo, VM_ContactInfo>();和
m.CreateMap<TAuthor, VM_Author>().ForMember(x => x.ContactInfo, opt => opt.MapFrom(o => o.ContactInfo));

var contacts = new List<TContactInfo>()
  {
  new TContactInfo() { Blog = "myblog", Email = "ws@qq.com" },
  new TContactInfo() { Blog = "myblog", Email = "ll@qq.com" }
  };
  TAuthor author = new TAuthor() { Description = "描述", Name = "吴双", ContactInfo = contacts };
  Mapper.Initialize(m =>
  {
  m.CreateMap<TContactInfo, VM_ContactInfo>();//注意 内部不同类型实体转换时必要的
  m.CreateMap<TAuthor, VM_Author>().ForMember(x => x.ContactInfo, opt => opt.MapFrom(o => o.ContactInfo));//注意 制定MapFrom是必要的
  });
  VM_Author dto = Mapper.Map<TAuthor, VM_Author>(author);

写在最后

在实体转换中,AutoMapper的必要性和实用性已经被你一览无余。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

更多精彩内容其他人还在看

asp.net 页面间传值与跳转的区别

通过Server.Transfer("b.aspx") 与Response.Redirect("b.aspx")的区别
收藏 0 赞 0 分享

ASP.NET Gridview与checkbox全选、全不选实现代码

ASP.NET Gridview checkbox全选与全不选实现代码,其实原理就是利用js来实现的,但需要简单的设置下回传。
收藏 0 赞 0 分享

ASP.NET DropDownList控件的使用方法

ASP.NET DropDownList控件的使用方法,学习asp.net的朋友没用过这个控件的朋友可以参考下。
收藏 0 赞 0 分享

一些.NET对多线程异常处理技巧分享

多线程应用,在实际的项目或产品开发中,原则上来说,应该尽量避免,但是在强调用户体验的要求下或开发平台的限制下(如 Silverlight Socket 通讯),我们不得不用多线程。
收藏 0 赞 0 分享

ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法

同一相站点,有些页面的客户端验证能工作,而有些死活不行。打开页面就出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null错误
收藏 0 赞 0 分享

asp.net 通用分页显示辅助类(改进版)

在使用ASP.NET编程时,如果不用控件的默认分页功能,想自己搞一个,可以看看本文的asp.net通用分页显示辅助类哦。
收藏 0 赞 0 分享

微软 Visual Studio 2010官方下载地址给大家

昨天VS2010在网上报道都已经发布了,现在今天在网上找到Visual Studio 2010官方下载地址,提供给大家下载。
收藏 0 赞 0 分享

Javascript 直接调用服务器C#代码 ASP.NET Ajax实例

近来总有一些朋友会问到一些入门的问题,把这些问题整理一下,写出来。在以前的文章里,曾经利用纯JS编写过Ajax引擎,在真正开发的时候,大家都不喜欢以这种低效率的方式开发,利用MS Ajax的集成的引擎,可以简单不少工作。
收藏 0 赞 0 分享

ASP.NET 页面刷新的实现方法(包括html,js)

ASP.NET 页面刷新的实现方法,比较全了, 包括html与js下的实现方法。
收藏 0 赞 0 分享

asp.net 无刷新翻页就是这么简单

前两天看了一个自定义分页控件,和AspNetPager一样是实现IPostBackEventHandler接口,不过简洁许多,就想能不能实现ICallbackEventHandler接口做到无刷新分页呢?想到了就马上去做,终于,设想变成了现实!!
收藏 0 赞 0 分享
查看更多