.NET CORE中使用AutoMapper进行对象映射的方法
更新时间:2019-06-25 03:01:38 作者:佚名 我要评论(0)
AutoMapper uses a fluent configuration API to define an object-object mapping strategy. AutoMapper uses a convention-based matching algorit
简介
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.
官网:http://automapper.org/
文档: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);
集合对象转换
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。
您可能感兴趣的文章:
- 五步掌握OOM框架AutoMapper基本使用
- .NET Core中依赖注入AutoMapper的方法示例
- 详解c# AutoMapper 使用方式
- Automapper实现自动映射的实例代码
相关文章
.NET CORE中使用AutoMapper进行对象映射的方法
简介 AutoMapper uses a fluent configuration API to define an object-object mapping strategy. AutoMapper uses a convention-based matching algorit2019-06-25.NET Core Dapper操作mysql数据库的实现方法
前言 现在ORM盛行,市面上已经出现了N款不同的ORM套餐了。今天,我们不谈EF,也不聊神马黑马,就说说 Dapper。如何在.NET Core中使用Dapper操作Mysql数据库呢2019-06-25详解.Net Core 权限验证与授权(AuthorizeFilter、ActionFilterAttribute)
在.Net Core 中使用AuthorizeFilter或者ActionFilterAttribute来实现登录权限验证和授权 一、AuthorizeFilter 新建授权类AllowAnonymous继承AuthorizeFilt2019-06-25
最新评论