五步掌握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的必要性和實用性已經被你一覽無余。
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關文章
ASP.NET?MVC前臺動態(tài)添加文本框并在后臺使用FormCollection接收值
這篇文章介紹了ASP.NET?MVC前臺動態(tài)添加文本框并在后臺使用FormCollection接收的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08asp.net中Post表單保存頁面狀態(tài)并輸出源碼的實現(xiàn)方法
先執(zhí)行腳本,復制源碼到隱藏域里,再輸出源碼,注意代碼紅色設置2012-08-08Visual Studio 2013+OpenCV2.4.10環(huán)境搭建教程
這篇文章主要為大家詳細介紹了Visual Studio 2013+OpenCV2.4.10環(huán)境搭建教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01Windows虛擬主機與VPS如何實現(xiàn)301重定向(asp.net)
301重定向應該是研究SEO必須掌握的技術。如果你是剛接觸SEO的菜鳥,想了解什么是301重定向,請看《html實現(xiàn)301重定向的方法》一文,我在該篇隨筆中引用了Google網站站長工具對301重定向的解釋2011-12-12Path類與Directory類與File類對路徑/目錄/文件的操作實例
本文將詳細介紹下:Path對路徑字符串進行操作/Directory和DirectoryInfo 對目錄進行操作/File和FileInfo對文件進行操作,感興趣的你可不要錯過了哈2013-02-02ASP.NET MVC5+EF6+EasyUI后臺管理系統(tǒng) 微信公眾平臺開發(fā)之資源環(huán)境準備
這篇文章主要介紹了ASP.NET MVC5+EF6+EasyUI后臺管理系統(tǒng),微信公眾平臺開發(fā)之資源環(huán)境準備,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09