【转】AutoMapper对象映射
什么是AutoMapper?
AutoMapper是一个简单的小型库,用于解决一个看似复杂的问题 -
摆脱将一个对象映射到另一个对象的代码。这种类型的代码是相当沉闷和无聊的写,所以为什么不发明一个工具来为我们做?
我们来看看在.netcore3.1中怎样使用AutoMapper9.0。
public class BasicProfile : Profile, IProfile
{
public BasicProfile()
{
CreateMap<TestDto, Test>();
CreateMap<Test, TestDto>();
}
}
Profile提供了一个命名的映射类,所有继承自Profile类的子类都是一个映射集合。这里我创建了一个BasicProfile继承Profile类。
CreateMap创建映射规则。
IProfile创建影射类的约束,表示继承自该接口的类为映射集合。
由于AutoMapper9.0中取消了自动创建影射规则的方法这里我们需要自己写一个:
public static class ServiceCollectionExtensions
{
/// <summary>
/// 自动创建映射
/// </summary>
/// <param name="services"></param>
public static void AddAutoMapper(this IServiceCollection services)
{
var allProfile = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll").Select(Assembly.LoadFrom)
.SelectMany(y => y.DefinedTypes)
.Where(p => p.GetInterfaces().Contains(typeof(IProfile)))
.ToArray();
services.AddAutoMapper(allProfile);
}
}
添加到ConfigureServices:
public void ConfigureServices(IServiceCollection services)
{
//自动创建映射
services.AddAutoMapper();
}
这样AutoMapper就配置完成,但为了方便调用,我们继续写几个扩展:
public static class AutoMapperApplicationBuilderExtensions
{
private static IServiceProvider _serviceProvider;
public static void UseStateAutoMapper(this IApplicationBuilder applicationBuilder)
{
_serviceProvider = applicationBuilder.ApplicationServices;
} public static TDestination Map<TDestination>(object source)
{
var mapper = _serviceProvider.GetRequiredService<IMapper>();
return mapper.Map<TDestination>(source);
} public static TDestination Map<TSource, TDestination>(TSource source)
{
var mapper = _serviceProvider.GetRequiredService<IMapper>(); return mapper.Map<TSource, TDestination>(source);
} public static TDestination MapTo<TSource, TDestination>(this TSource source)
{
var mapper = _serviceProvider.GetRequiredService<IMapper>();
return mapper.Map<TSource, TDestination>(source);
} public static TDestination MapTo<TDestination>(this object source)
{
var mapper = _serviceProvider.GetRequiredService<IMapper>();
return mapper.Map<TDestination>(source);
} public static List<TDestination> MapToList<TDestination>(this IEnumerable source)
{
var mapper = _serviceProvider.GetRequiredService<IMapper>();
return mapper.Map<List<TDestination>>(source);
} public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
{
var mapper = _serviceProvider.GetRequiredService<IMapper>();
return mapper.Map<List<TDestination>>(source);
}
}
添加到Configure:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStateAutoMapper();
}
使用:
public TestDto Get(string id)
{
var test = _testDomain.Get(id);
return test.MapTo<TestDto>();
}
转自:https://www.cnblogs.com/letnet/p/12205352.html
【转】AutoMapper对象映射的更多相关文章
- .NET之AutoMapper对象映射工具运用
AutoMapper对象映射工具:主要是将某一个实体转成另一个实体. 1.引用NuGet包;搜索:AutoMapper 2.创建实体类 using System; using System.Colle ...
- EF架构~AutoMapper对象映射工具简化了实体赋值的过程
回到目录 AutoMapper是一个.NET的对象映射工具,一般地,我们进行面向服务的开发时,都会涉及到DTO的概念,即数据传输对象,而为了减少系统的负载,一般我们不会把整个表的字段作为传输的数据,而 ...
- .NetCore学习笔记:四、AutoMapper对象映射
什么是AutoMapper?AutoMapper是一个简单的小型库,用于解决一个看似复杂的问题 - 摆脱将一个对象映射到另一个对象的代码.这种类型的代码是相当沉闷和无聊的写,所以为什么不发明一个工具来 ...
- 对象映射工具AutoMapper介绍
AutoMapper是用来解决对象之间映射转换的类库.对于我们开发人员来说,写对象之间互相转换的代码是一件极其浪费生命的事情,AutoMapper能够帮助我们节省不少时间. 一. AutoMapper ...
- .NET的对象映射工具AutoMapper使用笔记
AutoMapper是一个.NET的对象映射工具. 项目地址:https://github.com/AutoMapper/AutoMapper. 帮助文档:https://github.com/Aut ...
- 一文为你详细讲解对象映射库【AutoMapper】所支持场景
前言 在AutoMapper未出世前,对象与对象之间的映射,我们只能通过手动为每个属性一一赋值,时间长了不仅是我们而且老外也觉得映射代码很无聊啊.这个时候老外的所写的强大映射库AutoMapper横空 ...
- ASP.NET Core 中的对象映射之 AutoMapper
目录 AutoMapper 简介 AutoMapper 使用 初始化 Profile设置 扁平化映射 集合映射 投影 条件映射 值转换 设置转换前后行为 配置验证及设置 反向映射 自定义转换器 自定义 ...
- ASP.NET CORE 中使用AutoMapper进行对象映射
ASP.NET CORE 中使用AutoMapper进行对象映射 1.什么是AutoMapper? AutoMapper是基于对象到对象约定的映射工具,常用于(但并不仅限制于)把复杂的对象模型转为DT ...
- 【5min+】 对象映射只有AutoMapper?试试Mapster
系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...
随机推荐
- 【转】干货分享-100个shell脚本
本文用于记录学习和日常中使用过的shell脚本 [脚本1]打印形状 打印等腰三角形.直角三角形.倒直角三角形.菱形 #!/bin/bash # 等腰三角形 read -p "Please i ...
- 越南FCK批量拿站
关键词:inurl:detail_product.asp?lang= /FCKeditor/_samples/asp/sample01.asp/FCKeditor/_samples/asp/sampl ...
- jqGrid不支持IE8的解决办法
参考:https://blog.csdn.net/tarataotao/article/details/10376657
- 如何在cmd中连接数据库
数据库连接时遇到的问题 : https://www.cnblogs.com/xyzdw/archive/2011/08/11/2135227.htmlping +ip地址: 查看本机ip:ipconf ...
- Java HotSpot(TM) Client VM 与 server VM 的配置
在Linux 6.5 下安装Elasticsearch 出现错误: JVM is using the client VM [Java HotSpot(TM) Client VM] but should ...
- Maven项目-控制台乱码
乱码显示: 解决方法:
- c++中的全排列
next_permutation函数 组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_permutation(start,end),和prev_permutation(start, ...
- qrcode在手机上不显示的问题
可以试试以下解决方案: 1.修改qrcode.min.js:里的function n()红线区域替换成这个 , 原因是这样子才能支持安卓机显示.
- Java得到一个整数的绝对值,不使用任何判断和比较语句,包括API.
/** * Java得到一个整数的绝对值,不使用任何判断和比较语句,包括API. <br> * 1.不得使用任何API,如Math.abs()等.<br> * 2.不得使用判断 ...
- leetcode206 Reverse Linked List
""" Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL ...