OOM框架AutoMapper怎么用

发布时间:2021-03-11 15:25:12 作者:小新
来源:亿速云 阅读:140

小编给大家分享一下OOM框架AutoMapper怎么用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

预备

首先我们预备一些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);

看完了这篇文章,相信你对“OOM框架AutoMapper怎么用”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. .NET Core中使用OOM框架,AutoMapper的使用介绍
  2. C#中如何使用AutoMapper

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

oom框架 automapper

上一篇:建设企业网站时如何做好内容定位

下一篇:如何使用canvas设计出一个简易的画板

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》