本文摘自PHP中文网,作者Y2J,侵删。
本文主要介绍了OOM框架AutoMapper的相关知识,本篇的五个实例可以帮你解决常见的基本问题。具有一定的参考价值,下面跟着小编一起来看下吧写在前面
OOM顾名思义,Object-Object-Mapping实体间相互转换,AutoMapper也是个老生常谈了,其意义在于帮助你无需手动的转换简单而又麻烦的实体间关系,比如ViewModel和entity的转换,SearchModel和Entity的转换,我这篇分享的意义在于,网上大多数的分享都是几年前的,很多方法已经被废弃,到了编译器里会告诉你该方法已经过时,废弃的,不建议使用的,比如Mapper.CreateMap等方法,当然老司机大多数直接就去github看文档了,或者google一下就了解了,但是中文资料关于方法废弃后,并没有什么说明了。本篇的五个实例可以帮你解决常见的基本问题.
预备
首先我们预备一些ViewModel和TModel。ViewModel就是你和用户交互的实体。TModel就是你与数据库打交道的实体。
实体展示如下:
TModel有如下三个简单的实体,他们有独立的实体,也有一对多的实体。
1 2 3 4 5 6 7 8 9 | 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 ; }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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如下三个:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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目标实体。那么你应该参照如下实例:
1 2 3 4 5 6 7 | 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属性值。
1 2 3 4 5 6 7 | 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对象中的匹配,而只需要配置你泛型对象的映射匹配关系。
1 2 3 4 5 | 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>.这个时候你可以选择忽略这个属性
1 2 3 4 5 6 | 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);
|
实体包含不同类型属性转换(指定属性Mapfrom)
当然你需要这个属性的时候,你可以不忽略他,而是使用MapFrom来进行特殊的指定,并且在类型不相同的时候,你要指定你两个类型间的映射匹配关系。正如下面实例中的
m.CreateMap<TContactInfo, VM_ContactInfo>();和
m.CreateMap<TAuthor, VM_Author>().ForMember(x => x.ContactInfo, opt => opt.MapFrom(o => o.ContactInfo));
1 2 3 4 5 6 7 8 9 10 11 12 | 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));
});
VM_Author dto = Mapper.Map<TAuthor, VM_Author>(author);
|
【相关推荐】
1. ASP免费视频教程
2. ASP教程
3. 李炎恢ASP基础视频教程
以上就是介绍OOM中AutoMapper的使用方法的详细内容!
相关阅读 >>
asp.net mvc中传参并绑定数据的实例教程
asp.net c#中application的用法教程
asp.net页脚制作详解
.net存储pdf、word和excel到数据库的方法详解
asp.net异步触发用法(ajax)
解决.net服务器发送http后设置不了内容类型的情况
asp.net在网站根目录下创建文件夹
[asp.net mvc 小牛之路]05 - 使用 ninject
asp.net core mvc中如何把二级域名绑定到特定的控制器上
用signair和push.js完成消息推送代码详解
更多相关阅读请进入《AutoMapper》频道 >>
清华大学出版社
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。
转载请注明出处:木庄网络博客 » 介绍OOM中AutoMapper的使用方法