欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

五步掌握OOM框架AutoMapper基本使用

 更新時(shí)間:2017年01月17日 11:54:29   作者:坦蕩  
本文主要介紹了OOM框架AutoMapper的相關(guān)知識(shí),本篇的五個(gè)實(shí)例可以幫你解決常見(jiàn)的基本問(wèn)題。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧

寫(xiě)在前面

OOM顧名思義,Object-Object-Mapping實(shí)體間相互轉(zhuǎn)換,AutoMapper也是個(gè)老生常談了,其意義在于幫助你無(wú)需手動(dòng)的轉(zhuǎn)換簡(jiǎn)單而又麻煩的實(shí)體間關(guān)系,比如ViewModel和entity的轉(zhuǎn)換,SearchModel和Entity的轉(zhuǎn)換,我這篇分享的意義在于,網(wǎng)上大多數(shù)的分享都是幾年前的,很多方法已經(jīng)被廢棄,到了編譯器里會(huì)告訴你該方法已經(jīng)過(guò)時(shí),廢棄的,不建議使用的,比如Mapper.CreateMap等方法,當(dāng)然老司機(jī)大多數(shù)直接就去github看文檔了,或者google一下就了解了,但是中文資料關(guān)于方法廢棄后,并沒(méi)有什么說(shuō)明了。本篇的五個(gè)實(shí)例可以幫你解決常見(jiàn)的基本問(wèn)題.

預(yù)備

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

實(shí)體展示如下:

TModel有如下三個(gè)簡(jiǎn)單的實(shí)體,他們有獨(dú)立的實(shí)體,也有一對(duì)多的實(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如下三個(gè):

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

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

單個(gè)實(shí)體轉(zhuǎn)換的時(shí)候,在屬性字段名稱(chēng)完全匹配的情況下,你只需指定兩個(gè)實(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);

請(qǐng)注意在AutoMapper5.x當(dāng)中,Initialize來(lái)初始化你的規(guī)則是首選的。

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

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

當(dāng)你需要對(duì)不同名稱(chēng)的字段來(lái)進(jìn)行映射的時(shí)候,請(qǐng)注意使用ForMember方法,第一個(gè)參數(shù)需要你制定所需特殊配置的目標(biāo)字段,第二個(gè)參數(shù)你則需要制定你對(duì)該字段屬性的操作,我選擇了它提供的MapFrom方法,意義在于告訴AutoMapper,我需要講目標(biāo)實(shí)體的City來(lái)源 指定為 源實(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)換的時(shí)候,你不需要配置目標(biāo)List與源List對(duì)象中的匹配,而只需要配置你泛型對(duì)象的映射匹配關(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í)體包含不同類(lèi)型屬性轉(zhuǎn)換(忽略屬性)

在實(shí)體包含不同類(lèi)型屬性的時(shí)候,比如TModel1中包含了一個(gè)List<TModel>,而你的ViewModel1中包含了一個(gè)List<ViewModel>.這個(gè)時(shí)候你可以選擇忽略這個(gè)屬性

 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,映射時(shí)將忽略該屬性 由于List<TContactInfo>()和List<VM_ContactInfo>() 是不同類(lèi)型,所以需要配置忽略或者是特殊映射,特殊映射例子看下方

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

當(dāng)然你需要這個(gè)屬性的時(shí)候,你可以不忽略他,而是使用MapFrom來(lái)進(jìn)行特殊的指定,并且在類(lèi)型不相同的時(shí)候,你要指定你兩個(gè)類(lèi)型間的映射匹配關(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)部不同類(lèi)型實(shí)體轉(zhuǎn)換時(shí)必要的
  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);

寫(xiě)在最后

在實(shí)體轉(zhuǎn)換中,AutoMapper的必要性和實(shí)用性已經(jīng)被你一覽無(wú)余。

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

最新評(píng)論