首页 技术 正文
技术 2022年11月13日
0 收藏 696 点赞 4,056 浏览 3728 个字

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);
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,893
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,422
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,240
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,054
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,683
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,720