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

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

1、什么是AutoMapper?

AutoMapper是基于对象到对象约定的映射工具,常用于(但并不仅限制于)把复杂的对象模型转为DTO,一般用于ViewModel模式和跨服务范畴。

AutoMapper给用户提供了便捷的配置API,就像使用约定来完成自动映射那样。

AutoMapper包含以下功能:平展、投影、配置验证、列表和数组、嵌套映射、自定义类型转换程序、自定义值转换程序 、自定义值格式程序 、Null值替换

AutoMapper是一款单向映射器。这意味着它并没有内建映射对象支持来回写至原始源,除非用户在更新映射对象之后明确地创建逆向反射。这需要 通过设计完成,因为让DTO回写到,比方说:域模型或其他东西,就会更改它的持久性,同时人们也认为它是反模式的。在这种解决方案中,命令消息在双向映射 中往往是更好的选择。然而,在某些特定环境中,有人可能会为双向映射辩解,比如:非常简单的CRUD应用程序。一个支持双向映射的框架就是Glue。

2、在ASP.NET CORE中使用AutoMapper

①在程序包管理控制台中执行命令安装依赖包:
PM> Install-Package AutoMapper -Version 8.1.1
PM> Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection -Version 6.1.1
 ②使用依赖关系注入从应用中引用的服务

在Startup类的ConfigureServices()方法中调用AddAutoMapper()服务:

1 public void ConfigureServices(IServiceCollection services)
2 {
3     services.AddAutoMapper();
4 }
③创建AutoMapper映射规则

添加AutoMapperConfigs类,继承Profile类

 1 using AutoMapper;
 2 using CodeFirst.Models;
 3 using DAL;
 4 
 5 namespace CodeFirst.Common
 6 {
 7     public class AutoMapperConfigs : Profile
 8     {
 9         //添加实体映射关系
10         public AutoMapperConfigs()
11         {
12             CreateMap<MainModel, MainInfoResult>();  //MainModel转MainInfoResult
13             CreateMap<MainInfoResult, MainModel>();  //MainInfoResult转MainModel
14         }
15     }
16 }
 ④在构造函数中注入IMapper
1 public class LoginController : Controller
2 {
3     private IMapper _mapper;
4 
5     public LoginController(IMapper mapper)
6     {
7         _mapper = mapper;
8     }
9 }
⑤使用AutoMapper进行对象转换

单个对象转换

1 public ActionResult RegistSend(MainModel model)
2 {
3     var dto = _mapper.Map<MainInfoResult>(model);//MainModel 转MainInfoResult
4     var re = _userdal.RegistSend(dto);//传入的对象为MainInfoResult
5     return Json(new { status = re.Status, message = re.Message });
6 }

集合转换

public List<Student> GetStudentList()
   {
       //模拟数据
       var StuList = new List<StuInfo>()
       {
           new StuInfo(){ Id=1,Name="张三",Sex="",Age=18 },
           new StuInfo(){ Id=2,Name="李四",Sex="",Age=19 },
           new StuInfo(){ Id=3,Name="王五",Sex="",Age=18 },
       };
       //对象集合转Dto集合.
       var Students = _mapper.Map<List<Student>>(StuList);
       return Students;
   }

AutoMapper功能十分强大,支持属性名不同,NULL值替换等等,在此只介绍简单的常用方法,有关详情见AutoMapper官网地址:http://automapper.org/

转载于:https://www.cnblogs.com/gygg/p/11286869.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是在 .NET 6 ASP.NET Core Web API 使用 AutoMap 的初始化和帮助类。 首先,你需要在你的项目添加 AutoMap 的 NuGet 包。在 Visual Studio ,右键点击项目,选择“Manage NuGet Packages”,然后搜索 AutoMap 并安装。 接下来,你需要创建一个帮助类来帮助你初始化 AutoMap。这个类可以是一个静态类,包含一个静态的初始化方法。这个初始化方法将会注册你的 Mapper 配置,以便在应用程序启动时自动执行。 ```csharp using AutoMapper; public static class AutoMapperConfig { public static void Initialize() { MapperConfiguration config = new MapperConfiguration(cfg => { // 在这里进行你的 Mapper 配置 cfg.CreateMap<SourceClass, DestinationClass>(); }); IMapper mapper = config.CreateMapper(); Mapper = mapper; } public static IMapper Mapper { get; private set; } } ``` 在你的 Startup.cs 文件,你可以在 ConfigureServices 方法调用这个初始化方法: ```csharp public void ConfigureServices(IServiceCollection services) { // 其他配置... AutoMapperConfig.Initialize(); } ``` 现在,你可以在你的控制器或其他服务注入 IMapper 接口,使用 AutoMap 进行对象映射了。 ```csharp using AutoMapper; public class MyController : ControllerBase { private readonly IMapper _mapper; public MyController(IMapper mapper) { _mapper = mapper; } public IActionResult MyAction() { SourceClass source = new SourceClass(); DestinationClass destination = _mapper.Map<DestinationClass>(source); // 其他代码... } } ``` 这样,你就可以在 .NET 6 ASP.NET Core Web API 使用 AutoMap 了。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值