OOM框架AutoMapper如何使用-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)OOM框架AutoMapper如何使用的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、網(wǎng)絡(luò)空間、營銷軟件、網(wǎng)站建設(shè)、廣安網(wǎng)站維護(hù)、網(wǎng)站推廣。

預(yù)備

首先我們預(yù)備一些ViewModel和TModel。ViewModel就是你和用戶交互的實(shí)體。TModel就是你與數(shù)據(jù)庫打交道的實(shí)體。

實(shí)體展示如下:

TModel有如下三個簡單的實(shí)體,他們有獨(dú)立的實(shí)體,也有一對多的實(shí)體。

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; }
 }

單個實(shí)體轉(zhuǎn)換

單個實(shí)體轉(zhuǎn)換的時候,在屬性字段名稱完全匹配的情況下,你只需指定兩個實(shí)體間的轉(zhuǎn)換規(guī)則,指定source源實(shí)體和destination目標(biāo)實(shí)體。那么你應(yīng)該參照如下實(shí)例:

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當(dāng)中,Initialize來初始化你的規(guī)則是選的。

在你指定轉(zhuǎn)換規(guī)則后,請使用Map方法,進(jìn)行轉(zhuǎn)換并輸出你的目標(biāo)實(shí)體。還有第一個參數(shù)代表SourceModel,第二個參數(shù)是DestinationModel.

單個實(shí)體不同名屬性轉(zhuǎn)換

當(dāng)你需要對不同名稱的字段來進(jìn)行映射的時候,請注意使用ForMember方法,第一個參數(shù)需要你制定所需特殊配置的目標(biāo)字段,第二個參數(shù)你則需要制定你對該字段屬性的操作,我選擇了它提供的MapFrom方法,意義在于告訴AutoMapper,我需要講目標(biāo)實(shí)體的City來源 指定為 源實(shí)體的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);

集合轉(zhuǎn)換

在集合間轉(zhuǎn)換的時候,你不需要配置目標(biāo)List與源List對象中的匹配,而只需要配置你泛型對象的映射匹配關(guān)系。

  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>());//這里僅需配置實(shí)體間的轉(zhuǎn)換,而不是實(shí)體集合的轉(zhuǎn)換
  List<VM_Address> res = Mapper.Map<List<TAddress>, List<VM_Address>>(addressList);

實(shí)體包含不同類型屬性轉(zhuǎn)換(忽略屬性)

在實(shí)體包含不同類型屬性的時候,比如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>() 是不同類型,所以需要配置忽略或者是特殊映射,特殊映射例子看下方

實(shí)體包含不同類型屬性轉(zhuǎn)換(指定屬性Mapfrom)

當(dāng)然你需要這個屬性的時候,你可以不忽略他,而是使用MapFrom來進(jìn)行特殊的指定,并且在類型不相同的時候,你要指定你兩個類型間的映射匹配關(guān)系。正如下面實(shí)例中的

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>();//注意 內(nèi)部不同類型實(shí)體轉(zhuǎn)換時必要的
  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);

感謝各位的閱讀!關(guān)于“OOM框架AutoMapper如何使用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

網(wǎng)站名稱:OOM框架AutoMapper如何使用-創(chuàng)新互聯(lián)
URL地址:http://muchs.cn/article16/csgedg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、品牌網(wǎng)站制作、移動網(wǎng)站建設(shè)ChatGPT、網(wǎng)站排名、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司