您现在的位置是:网站首页> 编程资料编程资料
.NET CORE中使用AutoMapper进行对象映射的方法_实用技巧_
2023-05-24
402人已围观
简介 .NET CORE中使用AutoMapper进行对象映射的方法_实用技巧_
简介
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(); CreateMap(); } } 在构造函数中注入你的IMapper
IMapper _mapper; public PoundListController(IMapper mapper) { _mapper = mapper; }单个对象转换
//typeof(model)="PoundSheetViewModel" DBPoundSheet dBPoundSheet = _mapper.Map(model);
集合对象转换
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。
相关内容
- 详解使用DotNet CLI创建自定义的WPF项目模板_实用技巧_
- 打造自己的.NET Core项目模板_实用技巧_
- 浅谈.net core 注入中的三种模式:Singleton、Scoped 和 Transient_实用技巧_
- .net core如何在网络高并发下提高JSON的处理效率详解_实用技巧_
- .net core并发下线程安全问题详解_实用技巧_
- .net 4.5部署到docker容器的完整步骤_实用技巧_
- 详解ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁_实用技巧_
- 详解.net core webapi 前后端开发分离后的配置和部署_实用技巧_
- 浅谈从ASP.NET Core2.2到3.0你可能会遇到这些问题_实用技巧_
- ASP.NET Core利用Jaeger实现分布式追踪详解_实用技巧_
