簡(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安裝
1
2
|
AutoMapper AutoMapper.Extensions.Microsoft.DependencyInjection //依賴注入AutoMapper,需要下載該包。 |
在Startup中添加AutoMapper
1
2
3
4
5
6
|
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); //添加對(duì)AutoMapper的支持 services.AddAutoMapper(); } |
創(chuàng)建AutoMapper映射規(guī)則
1
2
3
4
5
6
7
8
9
|
public class AutoMapperConfigs:Profile { //添加你的實(shí)體映射關(guān)系. public AutoMapperConfigs() { CreateMap<DBPoundSheet, PoundSheetViewModel>(); CreateMap<PoundSheetViewModel, DBPoundSheet>(); } } |
在構(gòu)造函數(shù)中注入你的IMapper
1
2
3
4
5
6
|
IMapper _mapper; public PoundListController(IMapper mapper) { _mapper = mapper; } |
單個(gè)對(duì)象轉(zhuǎn)換
1
2
|
//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ì)服務(wù)器之家的支持。
原文鏈接:https://www.cnblogs.com/wyt007/p/10123451.html