亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

寫在前面

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

預備

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

實體展示如下:

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

單個實體轉換

單個實體轉換的時候,在屬性字段名稱完全匹配的情況下,你只需指定兩個實體間的轉換規(guī)則,指定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來初始化你的規(guī)則是首選的。

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

單個實體不同名屬性轉換

當你需要對不同名稱的字段來進行映射的時候,請注意使用ForMember方法,第一個參數(shù)需要你制定所需特殊配置的目標字段,第二個參數(shù)你則需要制定你對該字段屬性的操作,我選擇了它提供的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);

寫在最后

在實體轉換中,AutoMapper的必要性和實用性已經被你一覽無余。

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關文章

最新評論