automapper自动创建映射_.NET CORE中使用AutoMapper进行对象映射的方法

简介

AutoMapper uses a fluent configuration API to define an object-object mapping strategy. AutoMapper uses a convention-based matching algorithm to match up source to destination values. AutoMapper is geared towards model projection scenarios to flatten complex object models to DTOs and other simple objects, whose design is better suited for serialization, communication, messaging, or simply an anti-corruption layer between the domain and application layer.

官网:http://automapper.org/

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

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

平台支持:

.NET 4.6.1+

.NET Standard 2.0+ https://docs.microsoft.com/en-us/dotnet/standard/net-standard

使用

Nuget安装

AutoMapper

AutoMapper.Extensions.Microsoft.DependencyInjection //依赖注入AutoMapper,需要下载该包。

在Startup中添加AutoMapper

public void ConfigureServices(IServiceCollection services)

{

services.AddMvc();

//添加对AutoMapper的支持

services.AddAutoMapper();

}

创建AutoMapper映射规则

public class AutoMapperConfigs:Profile

{

//添加你的实体映射关系.

public AutoMapperConfigs()

{

CreateMap();

CreateMap();

}

}

在构造函数中注入你的IMapper

IMapper _mapper;

public PoundListController(IMapper mapper)

{

_mapper = mapper;

}

单个对象转换

//typeof(model)="PoundSheetViewModel"

DBPoundSheet dBPoundSheet = _mapper.Map(model);

集合对象转换

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。

您可能感兴趣的文章:五步掌握OOM框架AutoMapper基本使用

.NET Core中依赖注入AutoMapper的方法示例

详解c# AutoMapper 使用方式

Automapper实现自动映射的实例代码

  • 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、付费专栏及课程。

余额充值