ASP.NET CORE 3.1 WebApi 引用AutoMapper

一、添加引用包

  <PackageReference Include="AutoMapper" Version="10.1.1" />
  <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />

二、创建实体和视图实体

public class DemoViewModel
{
    public string DemoName { get; set; }
}
public class Demo
{
    public int Id { get; set; }
    public string Name { get; set; }
}

三、映射关系配置

public class CustomProfile:Profile
{
    /// <summary>
    /// 配置构造函数,用来创建关系映射
    /// </summary>
    public CustomProfile()
    {
        CreateMap<Demo, DemoViewModel>().ForMember(d=>d.DemoName,o=>o.MapFrom(s=>s.Name));
        CreateMap<DemoViewModel, Demo>().ForMember(d=>d.Name,o=>o.MapFrom(s=>s.DemoName));
    }
}
public static class AutoMapperSetup
{
     public static void AddAutoMapperSetup(this IServiceCollection services)
     {
         if (services == null) throw new ArgumentNullException(nameof(services));

         services.AddAutoMapper(typeof(AutoMapperConfig));
         AutoMapperConfig.RegisterMappings();
     }
 }
public class AutoMapperConfig
{
     public static MapperConfiguration RegisterMappings()
     {
         return new MapperConfiguration(cfg =>
         {
             cfg.AddProfile(new CustomProfile());
         });
     }
 }
 #region AutoMapper
 services.AddAutoMapperSetup();
 #endregion

四、使用,在Serv层调用IMapper

public class DemoServ : IDemoServ
{
    private readonly IDemoRepo _demoRepo;
    private readonly IMapper _mapper;

    public DemoServ(IDemoRepo demoRepo,IMapper mapper)
    {
        _demoRepo = demoRepo;
        _mapper = mapper;
    }

    public DemoViewModel GetDemos()
    {
        return _mapper.Map<DemoViewModel>(_demoRepo.GetDemos());
    }
}
  • 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、付费专栏及课程。

余额充值