介紹OOM中AutoMapper的使用方法

來源:互聯網
上載者:User
本文主要介紹了OOM架構AutoMapper的相關知識,本篇的五個執行個體可以幫你解決常見的基本問題。具有一定的參考價值,下面跟著小編一起來看下吧

寫在前面

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

【相關推薦】

1. ASP免費視頻教程

2. ASP教程

3. 李炎恢ASP基礎視頻教程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.