.NET CORE中使用AutoMapper進(jìn)行對(duì)象映射的方法
簡(jiǎn)介
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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Global.asax的Application_BeginRequest實(shí)現(xiàn)url重寫無后綴的代碼
本文為大家詳細(xì)介紹下利用Global.asax的Application_BeginRequest 實(shí)現(xiàn)url重寫其無后綴,具體核心代碼如下,有需求的朋友可以參考下,希望對(duì)大家有所幫助2013-08-08
aspx超強(qiáng)木馬查殺與防范(web網(wǎng)馬)
下面代碼是一個(gè)aspx超強(qiáng)木馬,功能很多,大家在服務(wù)器上見到一定要小心2013-12-12
ASP.NET TreeView讀取數(shù)據(jù)庫實(shí)例
這篇文章主要介紹了ASP.NET TreeView讀取數(shù)據(jù)庫實(shí)例,有需要的朋友可以參考一下2013-11-11
WPF實(shí)現(xiàn)定時(shí)刷新UI界面功能
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)定時(shí)刷新UI界面功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Discuz!NT 3與asp.net 整合的實(shí)例教程
本次整合只針對(duì)NETSNS中的代碼做了少許修改,完成了基本的和論壇同步注冊(cè),登陸和注銷,信息獲取,信息修改。為的是給各位Discuz!NT API愛好者做一個(gè)簡(jiǎn)單的API事例,供大家參考。2009-11-11
使用JavaScript代碼實(shí)現(xiàn)各種數(shù)據(jù)控件的反選功能 不要只做拖控件的菜鳥
在我們做許多項(xiàng)目的時(shí)候,會(huì)用到反選這個(gè)功能,但是我一般使用C#代碼創(chuàng)建數(shù)組遍歷實(shí)現(xiàn)功能,今天我想換一種語言實(shí)現(xiàn)一下,于是我就用JavaScript研究了一下怎么實(shí)現(xiàn)這個(gè)功能2011-12-12
asp.net中js+jquery添加下拉框值和后臺(tái)獲取示例
這篇文章主要介紹了asp.net中js+jquery添加下拉框值和后臺(tái)獲取的具體實(shí)現(xiàn),需要的朋友可以參考下2014-05-05
ajax.net對(duì)數(shù)據(jù)庫的插入實(shí)例
ajax.net對(duì)數(shù)據(jù)庫的插入實(shí)例,需要的朋友可以參考一下2013-04-04

