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.html
官網:http://automapper.org/ios
文檔:https://automapper.readthedocs.io/en/latest/index.htmlgit
GitHub:https://github.com/AutoMapper/AutoMapper/blob/master/docs/index.rstgithub
平臺支持:app
AutoMapper AutoMapper.Extensions.Microsoft.DependencyInjection //依賴注入AutoMapper,須要下載該包。
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); //添加對AutoMapper的支持 services.AddAutoMapper(); }
public class AutoMapperConfigs:Profile { //添加你的實體映射關係. public AutoMapperConfigs() { CreateMap<DBPoundSheet, PoundSheetViewModel>(); CreateMap<PoundSheetViewModel, DBPoundSheet>(); } }
IMapper _mapper; public PoundListController(IMapper mapper) { _mapper = mapper; }
//typeof(model)="PoundSheetViewModel" DBPoundSheet dBPoundSheet = _mapper.Map<DBPoundSheet>(model);