.Net Core 中使用AutoMapper进行对象映射

  官网:http://automapper.org/

  文档:https://automapper.readthedocs.io/en/latest/index.html

  GitHub:https://github.com/AutoMapper/AutoMapper/blob/master/docs/index.rst

⒈安装相关依赖

  AutoMapper 

  AutoMapper.Extensions.Microsoft.DependencyInjection

⒉在Startup中添加AutoMapper

复制代码
 1         public void ConfigureServices(IServiceCollection services)
 2         {
 3             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
 4 
 5             //添加对AutoMapper的支持
 6             services.AddAutoMapper(cfg => 
 7             {
 8                 cfg.AddProfile<AutoMapperConfig>();
 9             }, AppDomain.CurrentDomain.GetAssemblies());
10         }
复制代码

⒊使用配置文件创建AutoMapper映射规则

复制代码
 1 using AutoMapper;
 2 using AutoMapperTest.Entities;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Text;
 6 
 7 namespace AutoMapperTest.AutoMapper
 8 {
 9     public class AutoMapperConfig:Profile
10     {
11         public AutoMapperConfig()
12         {
13             CreateMap<UsersInputDto, Users>().ForMember(d => d.username, u => u.MapFrom(s => s.uname))    //属性名称映射
14                                    .ForMember(d => d.password, u => u.MapFrom(s => s.pwd))  //属性名称映射
15                                    .ForMember(d => d.age, u => u.Condition(s => s.age >= 0 && s.age <= 120)) //对一些属性做映射判断
16                                    .BeforeMap((dto, ent) => ent.fullname = dto.firstname + "_" + dto.lastname)   //对一些属性做映射前处理
17                                    ;
18         }
19     }
20 }
复制代码

⒋在代码中使用AutoMapper

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using AutoMapper;
 6 using AutoMapperTest.Entities;
 7 using Microsoft.AspNetCore.Mvc;
 8 
 9 namespace AutoMapperCore.Controllers
10 {
11     public class UsersController : Controller
12     {
13         private readonly IMapper _mapper;
14         public UsersController(IMapper mapper)
15         {
16             this._mapper = mapper;
17         }
18         public IActionResult Index()
19         {
20             UsersInputDto input = new UsersInputDto()
21             {
22                 id = 1, firstname = "fan", lastname = "qi", uname = "fanqisoft", pwd = "admin", enabled = 1
23             };
24             
25             Users users = _mapper.Map<Users>(input);
26             return View();
27         }
28     }
29 }
复制代码

 

作者:奇

出处:https://www.cnblogs.com/fanqisoft/p/10800810.html

版权:本作品采用「本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。」许可协议进行许可。

posted @   SpringCore  阅读(1052)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
more_horiz
keyboard_arrow_up light_mode palette
选择主题
点击右上角即可分享
微信分享提示