.NET CORE 中使用AutoMapper进行对象映射
2018-12-15 15:13
281 查看
简介
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.
文档:https://automapper.readthedocs.io/en/latest/index.html
GitHub:https://github.com/AutoMapper/AutoMapper/blob/master/docs/index.rst
平台支持:
- .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(); //添加对AutoMapper的支持 services.AddAutoMapper(); }
创建AutoMapper映射规则
public class AutoMapperConfigs:Profile { //添加你的实体映射关系. public AutoMapperConfigs() { CreateMap<DBPoundSheet, PoundSheetViewModel>(); CreateMap<PoundSheetViewModel, DBPoundSheet>(); } }
在构造函数中注入你的IMapper
IMapper _mapper; public PoundListController(IMapper mapper) { _mapper = mapper; }
单个对象转换
//typeof(model)="PoundSheetViewModel" DBPoundSheet dBPoundSheet = _mapper.Map<DBPoundSheet>(model);
集合对象转换
相关文章推荐
- 使用AutoMapper进行对象间映射
- C#使用AutoMapper6.2.2.0进行对象映射
- ORM对象关系映射之使用GreenDAO进行CRUD操作
- 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十三 || DTOs 对象映射使用,项目打包部署Windows+Linux
- 使用Hibernate进行对象的关系映射
- .NET的对象映射工具AutoMapper使用笔记
- spring使用OXM进行对象XML映射解析
- 使用Hibernate进行对象的关系映射(2)
- Value Object(值对象)如何使用 EF 进行正确映射
- [转]对象映射类AutoMapper的使用
- 使用Hibernate进行对象的关系映射
- 使用Hibernate注解Annotations进行对象映射的异常处理
- ORM对象关系映射之使用GreenDAO进行CRUD操作
- 学习《spring 3.x企业应用开发实战》之使用OXM进行对象XML映射
- 使用Hibernate进行对象的关系映射(3)
- 使用Hibernate进行对象的关系映射(1)
- DDD 领域驱动设计-Value Object(值对象)如何使用 EF 进行正确映射
- ORM对象关系映射之使用GreenDAO进行CRUD操作
- 使用XMLHttprequest对象进行异步调用html页面的示例
- 使用文件映射的方式进行共享数据中CreateFileMapping、MapViewOfFile函数参数说明