什么是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对象映射的更多相关文章

  1. .NET之AutoMapper对象映射工具运用

    AutoMapper对象映射工具:主要是将某一个实体转成另一个实体. 1.引用NuGet包;搜索:AutoMapper 2.创建实体类 using System; using System.Colle ...

  2. EF架构~AutoMapper对象映射工具简化了实体赋值的过程

    回到目录 AutoMapper是一个.NET的对象映射工具,一般地,我们进行面向服务的开发时,都会涉及到DTO的概念,即数据传输对象,而为了减少系统的负载,一般我们不会把整个表的字段作为传输的数据,而 ...

  3. .NetCore学习笔记:四、AutoMapper对象映射

    什么是AutoMapper?AutoMapper是一个简单的小型库,用于解决一个看似复杂的问题 - 摆脱将一个对象映射到另一个对象的代码.这种类型的代码是相当沉闷和无聊的写,所以为什么不发明一个工具来 ...

  4. 对象映射工具AutoMapper介绍

    AutoMapper是用来解决对象之间映射转换的类库.对于我们开发人员来说,写对象之间互相转换的代码是一件极其浪费生命的事情,AutoMapper能够帮助我们节省不少时间. 一. AutoMapper ...

  5. .NET的对象映射工具AutoMapper使用笔记

    AutoMapper是一个.NET的对象映射工具. 项目地址:https://github.com/AutoMapper/AutoMapper. 帮助文档:https://github.com/Aut ...

  6. 一文为你详细讲解对象映射库【AutoMapper】所支持场景

    前言 在AutoMapper未出世前,对象与对象之间的映射,我们只能通过手动为每个属性一一赋值,时间长了不仅是我们而且老外也觉得映射代码很无聊啊.这个时候老外的所写的强大映射库AutoMapper横空 ...

  7. ASP.NET Core 中的对象映射之 AutoMapper

    目录 AutoMapper 简介 AutoMapper 使用 初始化 Profile设置 扁平化映射 集合映射 投影 条件映射 值转换 设置转换前后行为 配置验证及设置 反向映射 自定义转换器 自定义 ...

  8. ASP.NET CORE 中使用AutoMapper进行对象映射

    ASP.NET CORE 中使用AutoMapper进行对象映射 1.什么是AutoMapper? AutoMapper是基于对象到对象约定的映射工具,常用于(但并不仅限制于)把复杂的对象模型转为DT ...

  9. 【5min+】 对象映射只有AutoMapper?试试Mapster

    系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...

随机推荐

  1. 查漏补缺之Go的Strings, bytes, runes和字符

    字节遍历,字符遍历 https://play.golang.org/p/DeZcCN9aHXo package main import ( "fmt" "unicode/ ...

  2. Sublime设置html头部

    1.Ctrl + N,新建一个文档:2.Ctrl + Shift + P,打开命令模式,再输入 sshtml 进行模糊匹配,将语法切换到html模式:3.输入 !,再按下 Tab键或者 Ctrl + ...

  3. 使用CSS3动画属性实现各种旋转跳跃

    Transform字面上就是变形,改变的意思.在CSS3中transform主要包括以下几种:旋转rotate.扭曲skew.缩放scale和移动translate以及矩阵变形matrix. tran ...

  4. 傅盛读书笔记:下一个Moonshot是什么?

    猎豹移动CEO 傅盛 九月底,我有幸在硅谷拜访了苹果前CEO斯卡利.老人如今已经75岁高龄,但看起来仍充满活力.他花了一上午的时间跟我们沟通,非常谦和.平等.坦诚,给我留下了很深的印象.末了,给我们介 ...

  5. 在Windows中实现Java调用DLL(转载)

    本文提供调用本地 C 代码的 Java 代码示例,包括传递和返回某些常用的数据类型.本地方法包含在特定于平台的可执行文件中.就本文中的示例而言,本地方法包含在 Windows 32 位动态链接库 (D ...

  6. 集合set() 二

    集合分类    可变集合set() 可以添加和删除元素,非可哈希的(值set之后的整体),不能用作字典的键,也不能做其它集合的元素. set()  之后变为不可哈希的 不可变集合 frozenset( ...

  7. Docker 基础入门

    Docker是一个开放的平台,将应用和基础设施分隔开来, 方便快速的交付软件.利用docker的提供的方法可以快速的测试和部署代码,显著的减少写代码和部署直接的延迟. Docker 平台(The Do ...

  8. Linux CentOS7 VMware 特殊权限set_uid、特殊权限set_gid、特殊权限stick_bit、软链接文件、硬连接文件

    一.特殊权限set_uid root用户本身拥有对/etc/passwd的写权限,无可厚非:那普通用户呢,这里就用到了setuid,setuid的作用是“让执行该命令的用户以该命令拥有者的权限去执行” ...

  9. 「NOIP2014」联合权值

    传送门 Luogu 解题思路 因为这是一棵树,所以说两个点如果能产生联合权值,那么它们就只能通过唯一的一个中转点来匹配,所以我们就枚举这个中转点. 但是我们又会发现,如果把每个点周围的点抠出来进行两两 ...

  10. mysql 单表索引优化

    建表语句 CREATE TABLE IF NOT EXISTS `article` ( `id` INT(10) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMEN ...