五步掌握OOM框架AutoMapper基本使用
寫(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í)也希望多多支持腳本之家!
- ASP.NET Core使用AutoMapper實(shí)現(xiàn)實(shí)體映射
- .NET 6開(kāi)發(fā)TodoList應(yīng)用之使用AutoMapper實(shí)現(xiàn)GET請(qǐng)求
- C# 中AutoMapper的使用方法
- C# AutoMapper 使用方法總結(jié)
- .NET CORE中使用AutoMapper進(jìn)行對(duì)象映射的方法
- .NET Core中依賴(lài)注入AutoMapper的方法示例
- 詳解c# AutoMapper 使用方式
- Automapper實(shí)現(xiàn)自動(dòng)映射的實(shí)例代碼
- AutoMapper實(shí)體映射基本用法
相關(guān)文章
ASP.NET?MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值
這篇文章介紹了ASP.NET?MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08asp.net無(wú)法加載oci.dll等錯(cuò)誤的解決方法
.net在windows2003下訪問(wèn)oracle9i提示“無(wú)法加載oci.dll”或"無(wú)法在dll oci.dll中找到名為ocienvcreate的入口點(diǎn) "的修復(fù)方法2013-10-10asp.net中Post表單保存頁(yè)面狀態(tài)并輸出源碼的實(shí)現(xiàn)方法
先執(zhí)行腳本,復(fù)制源碼到隱藏域里,再輸出源碼,注意代碼紅色設(shè)置2012-08-08Visual Studio 2013+OpenCV2.4.10環(huán)境搭建教程
這篇文章主要為大家詳細(xì)介紹了Visual Studio 2013+OpenCV2.4.10環(huán)境搭建教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Windows虛擬主機(jī)與VPS如何實(shí)現(xiàn)301重定向(asp.net)
301重定向應(yīng)該是研究SEO必須掌握的技術(shù)。如果你是剛接觸SEO的菜鳥(niǎo),想了解什么是301重定向,請(qǐng)看《html實(shí)現(xiàn)301重定向的方法》一文,我在該篇隨筆中引用了Google網(wǎng)站站長(zhǎng)工具對(duì)301重定向的解釋2011-12-12Path類(lèi)與Directory類(lèi)與File類(lèi)對(duì)路徑/目錄/文件的操作實(shí)例
本文將詳細(xì)介紹下:Path對(duì)路徑字符串進(jìn)行操作/Directory和DirectoryInfo 對(duì)目錄進(jìn)行操作/File和FileInfo對(duì)文件進(jìn)行操作,感興趣的你可不要錯(cuò)過(guò)了哈2013-02-02.Net使用RabbitMQ即時(shí)發(fā)消息Demo
RabbitMQ是一個(gè)在AMQP基礎(chǔ)上完整的,可復(fù)用的企業(yè)消息系統(tǒng),下面這篇文章主要給大家介紹了關(guān)于.Net使用RabbitMQ即時(shí)發(fā)消息的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07ASP.NET MVC5+EF6+EasyUI后臺(tái)管理系統(tǒng) 微信公眾平臺(tái)開(kāi)發(fā)之資源環(huán)境準(zhǔn)備
這篇文章主要介紹了ASP.NET MVC5+EF6+EasyUI后臺(tái)管理系統(tǒng),微信公眾平臺(tái)開(kāi)發(fā)之資源環(huán)境準(zhǔn)備,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09