如何在ASP.NET Core中设置Automapper [英] How to set up Automapper in ASP.NET Core

查看:122
本文介绍了如何在ASP.NET Core中设置Automapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NET方面相对较新,因此我决定使用.NET Core,而不是学习老方法".我找到了有关为.NET Core设置AutoMapper的详细文章在这里,但是对于新手来说,还有更简单的演练吗?

I'm relatively new at .NET, and I decided to tackle .NET Core instead of learning the "old ways". I found a detailed article about setting up AutoMapper for .NET Core here, but is there a more simple walkthrough for a newbie?

推荐答案

我知道了!详细信息如下:

I figured it out! Here's the details:

  1. 通过 NuGet 将主AutoMapper软件包添加到您的解决方案中.

  1. Add the main AutoMapper Package to your solution via NuGet.

通过 NuGet .

为映射配置文件创建一个新类. (我在主解决方案目录中创建了一个名为MappingProfile.cs的类,并添加了以下代码.)我将使用UserUserDto对象作为示例.

Create a new class for a mapping profile. (I made a class in the main solution directory called MappingProfile.cs and add the following code.) I'll use a User and UserDto object as an example.

 public class MappingProfile : Profile {
     public MappingProfile() {
         // Add as many of these lines as you need to map your objects
         CreateMap<User, UserDto>();
         CreateMap<UserDto, User>();
     }
 }

  • 然后在Startup.cs中添加AutoMapperConfiguration,如下所示:

  • Then add the AutoMapperConfiguration in the Startup.cs as shown below:

     public void ConfigureServices(IServiceCollection services) {
         // .... Ignore code before this
    
        // Auto Mapper Configurations
         var mapperConfig = new MapperConfiguration(mc =>
         {
             mc.AddProfile(new MappingProfile());
         });
    
         IMapper mapper = mapperConfig.CreateMapper();
         services.AddSingleton(mapper);
    
         services.AddMvc();
    
     }
    

  • 要在代码中调用映射的对象,请执行以下操作:

  • To invoke the mapped object in code, do something like the following:

     public class UserController : Controller {
    
         // Create a field to store the mapper object
         private readonly IMapper _mapper;
    
         // Assign the object in the constructor for dependency injection
         public UserController(IMapper mapper) {
             _mapper = mapper;
         }
    
         public async Task<IActionResult> Edit(string id) {
    
             // Instantiate source object
             // (Get it from the database or whatever your code calls for)
             var user = await _context.Users
                 .SingleOrDefaultAsync(u => u.Id == id);
    
             // Instantiate the mapped data transfer object
             // using the mapper you stored in the private field.
             // The type of the source object is the first type argument
             // and the type of the destination is the second.
             // Pass the source object you just instantiated above
             // as the argument to the _mapper.Map<>() method.
             var model = _mapper.Map<UserDto>(user);
    
             // .... Do whatever you want after that!
         }
     }
    

  • 我希望这可以帮助某人从ASP.NET Core入手!我欢迎任何反馈或批评,因为我还是.NET世界的新人!

    I hope this helps someone starting fresh with ASP.NET Core! I welcome any feedback or criticisms as I'm still new to the .NET world!

    这篇关于如何在ASP.NET Core中设置Automapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆