如何在 .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()
值得注意的是,上面的示例是一种 单向流动,这是什么意思呢?举个例子吧,下面是 单向流动 的一段代码。
- 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
相关文章
- 企业在跨国传输大数据会遇到哪些困难?
- TT WP100 水冷专用机箱
- 字节跳动的Pico,能追上Meta的Oculus吗?
- 教育机器人是新的“智商税”吗?
- 电脑竟能开机自动工作?原来用了这样的远程神器
- 全球首个AI框架CC EAL2+证书 ,昇思MindSpore推开可信AI大门
- OPPO小布4.0:软件定义硬件,智能定义“助手”
- 影驰 HOF II DDR4 4000内存评测:极限频率与白色美学的结合
- gskill 大RGB时代,3200C14D-16GTZR简测
- 24核心叫板旗舰i9 AMD锐龙ThreadRipper 2970WX首发详细图文评测
- Wework创始人再创业,靠美版“自如”估值10亿美金
- 三星电视“上新”在即,将有哪些新杀器?
- 华硕ROG Zenith Extreme主板评测:地表最强X399
- 人工智能职业教育怎么搞?操作系统层级的解法来了
- 技嘉GTX 1650图赏
- 【转】产品运营
- 困在赛博世界里的“仿生机器人”
- 客群分流、运营成本攀升,百货零售业如何实现“人货场”全盘数字化升级?
- 【转】决定项目生死的区块链运营
- 高明的决策和投资远比低效的勤奋更重要 - 人生感悟