写在前面
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的必要性和实用性已经被你一览无余。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持站长技术!
前言 相信大家都知道在IDE中代码的智能提示几乎都是标配,虽然一些文本编辑器也...
vbs:把一段文字中指定字符颜色变成红色的正则 functionc(Tstr,Word) Dimre Setre...
微信文件传输助手是微信电脑版与手机微信之间相互传输图片等文件的好工具,但很...
歌词编辑器 歌词编辑器 第一步:选择要播放的歌曲并播放 第二步:填写全部的歌词...
一、正则表达式概述 二、正则表达式在VBScript中的应用 三、正则表达式在VavaScr...
【排序算法】之lowb三人组冒泡、插入、选择 什么是lowb三人组 冒泡排序bubble so...
ADO对象: Connection Command Recordset Record Stream ASP支持的对象很多,可...
计算属性computed: 支持缓存,只有依赖数据发生改变,才会重新进行计算 不支持...
一石激起千层浪,继中国区浩浩荡荡的大裁员告一段落之后,甲骨文并未因此收起手...
本文将研究 ES6 的 for ... of 循环。 旧方法 在过去,有两种方法可以遍历 javas...