如何在 .NetCore 中使用 AutoMapper 高级功能

本文转载自微信公众号「码农读书」,中使作者码农读书。中使转载本文请联系码农读书公众号。中使
AutoMapper 是中使一个基于约定的面向对象的映射器,它的中使功能常用于将一个 input 对象 转成一个不同类型的 output 对象,input 和 output 对象之间的中使属性可能相同也可能不相同,这一篇我们来一起研究一下 AutoMapper 的中使一些高级玩法。
安装 AutoMapper
要想在项目中使用 AutoMapper ,中使需要通过 nuget 引用 AutoMapper 和 AutoMapper.Extensions.Microsoft.DependencyInjection 包,中使可以通过 Visual Studio 2019 的中使 NuGet package manager 可视化界面安装 或者 通过 NuGet package manager 命令行工具输入以下命令:
Install-Package AutoMapper Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection配置 AutoMapper
一旦 AutoMapper 成功安装之后,接下来就可以将它引入到 ServiceCollection 容器中,中使如下代码所示:
public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddAutoMapper(typeof(AuthorProfile)); }使用 profiles 统一管理 mapping 信息
可以使用 profiles 来统一组织你的中使 mapping 信息,要创建 profile,中使需要实现 AutoMapper 提供的中使 Profile 类,然后在你刚才创建的中使 Profile 子类的构造函数中添加映射信息,下面的代码展示了如何创建一个从 Proifle 继承的 AuthorProfile 类以及相关信息。
public class AuthorProfile : Profile { public AuthorProfile() { CreateMap<AuthorModel, AuthorDTO>(); } }接下来再看 AuthorModel 和 AuthorDTO 两个对象的定义:
public class AuthorModel { public int Id { get; set; } public string FirstName { get;set; } public string LastName { get; set; } public string Address { get; set; } } public class AuthorDTO { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; }使用 ReverseMap()
值得注意的是,b2b信息网上面的示例是一种 单向流动,这是什么意思呢?举个例子吧,下面是 单向流动 的一段代码。
AutoMapper.Mapper.CreateMap<AuthorDTO, AuthorModel>();有了这个 Map,接下来就可以轻松实现 AuthorDTO 到 AuthorModel 的转换,代码如下:
var authorModel = AutoMapper.Mapper.Map<AuthorModel>(author);假设因为某种原因,你需要将 authorModel 实例反转成 authorDTO,这时你用了如下的代码段。
var author = AutoMapper.Mapper.Map(authorModel);很遗憾,这种方式注定会抛出异常,这是因为 AutoMapper 并不知道如何实现 authorModel 到 authorDTO 的转换,毕竟你没有定义此种 map 的映射流向,那怎么解决呢?可以再定义一个 CreateMap 映射哈,其实没必要,简单粗暴的做法就是调用 ReverseMap 即可,实现代码如下:
AutoMapper.Mapper.CreateMap<AuthorDTO, AuthorModel>().ReverseMap();使用 ForMember() 和 MapFrom()
这一节我们继续使用之前说到的 AuthorModel 和 AuthorDTO 类,下面的代码片段展示了如何将 AuthorModel 转成 AuthorDTO 。
var author = new AuthorModel(); author.Id = 1; author.FirstName = "Joydip"; author.LastName = "Kanjilal"; author.Address = "Hyderabad"; var authorDTO = _mapper.Map<AuthorDTO>(author);现在假设我将 AuthorModel 中的 Address 改成 Address1,亿华云如下代码所示:
public class AuthorModel { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address1 { get; set; } }然后在 AuthorProfile 中更新一下 mapping 信息,如下代码所示:
public class AuthorProfile : Profile { public AuthorProfile() { CreateMap<AuthorModel, AuthorDTO>().ForMember(destination => destination.Address, map => map.MapFrom(source => source.Address1)); } }使用 NullSubstitute
何为 NullSubstitute 呢?大意就是在映射转换的过程中,将input 为null 的属性映射之后做自定义处理,比如在 ouput 中改成 No Data,下面的代码展示了如何去实现。
AutoMapper.Mapper.CreateMap<AuthorModel, AuthorDTO>().ForMember(destination => destination.Address, opt => opt.NullSubstitute("No data"));mapping 的 AOP 拦截
考虑下面的两个类。
public class OrderModel { public int Id { get; set; } public string ItemCode { get; set; } public int NumberOfItems { get; set; } } public class OrderDTO { public int Id { get; set; } public string ItemCode { get; set; } public int NumberOfItems { get; set; } }可以使用 BeforeMap() 在 源对象 或者 目标对象 上执行一些计算或者初始化成员操作,下面的代码展示了如何去实现。
Mapper.Initialize(cfg => { cfg.CreateMap().BeforeMap((src, dest) => src.NumberOfItems = 0) });当 mapping 执行完之后,可以在 目标对象 上 安插 AfterMap() 方法,下面的代码展示了如何去实现。
public OrderDTO MapAuthor(IMapper mapper, OrderDTO orderDTO) { return mapper.Map<OrderModel, OrderDTO>(orderDTO, opt => { opt.AfterMap((src, dest) => { dest.NumberOfItems = _orderService.GetTotalItems(src); }); }); }使用嵌套映射
AutoMapper 同样也可以使用嵌套映射,考虑下面的 domain 类。
public class Order { public string OrderNumber { get; set; } public IEnumerable<OrderItem> OrderItems { get; set; } } public class OrderItem { public string ItemName { get; set; } public decimal ItemPrice { get; set; } public int ItemQuantity { get; set; } }接下来再看一下 DTO 类。
public class OrderDto { public string OrderNumber { get; set; } public IEnumerable<OrderItemDto> OrderItems { get; set; } } public class OrderItemDto { public string ItemName { get; set; } public decimal ItemPrice { get; set; } public int ItemQuantity { get; set; } }最后看看如何在转换的过程中使用 mapping 的。
var orders = _repository.GetOrders(); Mapper.CreateMap<Order, OrderDto>(); Mapper.CreateMap<OrderItem, OrderItemDto>(); var model = Mapper.Map<IEnumerable<Order>, IEnumerable<OrderDto>>(orders);AutoMapper 让你用最小化的配置实现了对象之间的映射,同时也可以实现自定义的解析器来实现具有完全不同结构对象之间的映射,自定义解析器可以生成与目标对象具有相同结构的服务器租用exchange,以便AutoMapper在运行时可以据其实现映射。
译文链接:https://www.infoworld.com/article/3406800/more-advanced-automapper-examples-in-net-core.html
相关文章
探索iPhonePlus系列的魅力——以a1524的iPhone6Plus为例
摘要:a1524的iPhone6Plus作为iPhonePlus系列中的一员,拥有出色的大屏幕、强劲的性能和精致的设计,成为引领移动设备市场的佼佼者。本文将从多个角度探索a1524的iP...2025-11-05- 复制lpq[l][P][user] 1.2025-11-05
前段时间,北京市海淀区人民检察院出了一个《网络安全刑事司法保护白皮书》,根据里面的统计结果目前的黑产犯罪大部分还是呈现低龄、低学历的特征。但是下面要讲的这个故事,学霸来搞黑产,听说一次就捞了6个亿,咱2025-11-05- 复制tomcat文件夹下的/conf/web.xml文件插入 readonly false 1.2.3.2025-11-05
电脑经常出现10061错误的原因及解决方法(深入探讨10061错误,帮助您解决电脑连接问题)
摘要:随着计算机技术的发展,电脑已成为我们生活和工作中必不可少的一部分。然而,在使用电脑过程中,我们有时会遇到各种问题,其中之一就是10061错误。本文将围绕这一主题,详细介绍10061...2025-11-05
本文收集了一些 Redis 使用中经常遇到的一些问题,和与之相对应的解决方案,这些内容不但会出现在实际工作中,也是面试的高频问题,接下来一起来看。一、缓存雪崩缓存雪崩是指在短时间内,有大量缓存同时过期2025-11-05

最新评论