.NET CORE中使用AutoMapper進(jìn)行對(duì)象映射的方法
簡介
AutoMapper uses a fluent configuration API to define an object-object mapping strategy. AutoMapper uses a convention-based matching algorithm to match up source to destination values. AutoMapper is geared towards model projection scenarios to flatten complex object models to DTOs and other simple objects, whose design is better suited for serialization, communication, messaging, or simply an anti-corruption layer between the domain and application layer.
官網(wǎng):http://automapper.org/
文檔:https://automapper.readthedocs.io/en/latest/index.html
GitHub:https://github.com/AutoMapper/AutoMapper/blob/master/docs/index.rst
平臺(tái)支持:
- .NET 4.6.1+
- .NET Standard 2.0+ https://docs.microsoft.com/en-us/dotnet/standard/net-standard
使用
Nuget安裝
AutoMapper AutoMapper.Extensions.Microsoft.DependencyInjection //依賴注入AutoMapper,需要下載該包。
在Startup中添加AutoMapper
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); //添加對(duì)AutoMapper的支持 services.AddAutoMapper(); }
創(chuàng)建AutoMapper映射規(guī)則
public class AutoMapperConfigs:Profile { //添加你的實(shí)體映射關(guān)系. public AutoMapperConfigs() { CreateMap<DBPoundSheet, PoundSheetViewModel>(); CreateMap<PoundSheetViewModel, DBPoundSheet>(); } }
在構(gòu)造函數(shù)中注入你的IMapper
IMapper _mapper; public PoundListController(IMapper mapper) { _mapper = mapper; }
單個(gè)對(duì)象轉(zhuǎn)換
//typeof(model)="PoundSheetViewModel" DBPoundSheet dBPoundSheet = _mapper.Map<DBPoundSheet>(model);
集合對(duì)象轉(zhuǎn)換
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
asp.net實(shí)現(xiàn)簡單分頁實(shí)例
這篇文章主要介紹了asp.net實(shí)現(xiàn)簡單分頁的方法,實(shí)例分析了asp.net分頁的基本原理與實(shí)現(xiàn)流程,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01動(dòng)態(tài)代理的5模式使用示例和Mixin模式
什么叫"動(dòng)態(tài)代理",代理模式我們都知道,動(dòng)態(tài)代理就是動(dòng)態(tài)生成的代理(采用Emit)。5種代理模式:ClassProxy、ClassProxyWithTarget、InterfaceProxyWithoutTarget、InterfaceProxyWithTarget、InterfaceProxyWithTargetInterface、Mixin模式2013-11-11asp.net實(shí)現(xiàn)DataList與Repeater嵌套綁定的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)DataList與Repeater嵌套綁定的方法,結(jié)合實(shí)例形式分析了DataList與Repeater的步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-04-04asp.net 網(wǎng)絡(luò)硬盤實(shí)現(xiàn)分析
隨著網(wǎng)絡(luò)技術(shù)的日益普及和信息化建設(shè)的重視,網(wǎng)絡(luò)硬盤作為一種新型安全的網(wǎng)絡(luò)存儲(chǔ)系統(tǒng),已越來越受到人們的重視和喜歡。2011-02-02解析WPF綁定層次結(jié)構(gòu)數(shù)據(jù)的應(yīng)用詳解
本文講述WPF中單層次數(shù)據(jù)和多層次數(shù)據(jù)的綁定方法,主要闡述數(shù)據(jù)綁定的顯示層面,其中涉及了ListBox和Treeview控件。并說明它們之間的差異2013-05-05實(shí)例說明asp.net中的簡單角色權(quán)限控制
權(quán)限控制在信息管理中屬于基本功能,權(quán)限控制中其中以Window權(quán)限為模型的角色用戶(也稱用戶組用戶)模型使用較多。本文以網(wǎng)站管理后臺(tái)權(quán)限控制為例簡要說明。2009-10-10asp.net repeater實(shí)現(xiàn)批量刪除時(shí)注冊(cè)多選框id到客戶端
repeater批量刪除時(shí)注冊(cè)多選框id到客戶端的實(shí)現(xiàn)代碼2008-11-11Ext.Net學(xué)習(xí)筆記之button小結(jié)
Ext.Net學(xué)習(xí)筆記之button小結(jié),需要的朋友可以參考一下2013-02-02