Use AutoMapper in asp.net core 3.1.x

AutoMapper role

  • The function of AutoMapper is to transform one object into another object, avoiding to transform every time;
  • Use DTO to realize the decoupling of presentation layer and domain Model, and use AutoMapper to realize the mutual conversion between DTO and domain Model;
基于访问性的控制或从模型本身上考虑。对外开放的原则是,尽量降低系统耦合度,否则内部一旦变更外部所有的接口都要跟随发生变更;另外,系统内部的一些数据或方法并不希望外部能看到或调用。类似的考虑很多,只是举个例子。系统设计的原则是高内聚低耦合,尽量依赖抽象而不依赖于具体。这里感觉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.

AutoMapper supports the following platforms:
.NET 4.6.1+
.NET Standard 2.0+

Official website: http://automapper.org/
Document: https://automapper.readthedocs.io/en/latest/index.html
GitHub: https://github.com/AutoMapper/AutoMapper/blob/master/docs/index .rst

 

What is DTO?

DTO (Data Transfer Object) is a data transfer object (anemia model). To put it bluntly, it is an object, but it is all data.

Why use DTO?

  1. DTO pays more attention to data and reasonably encapsulates domain objects so as not to expose the behavior of domain objects to the presentation layer excessively;
  2. DTO is designed for the needs of UI, while the domain model is designed for business. Therefore, DTO is more suitable for interaction with the presentation layer. Through DTO, we realize the decoupling between the presentation layer and the domain Model, so changing the domain Model will not affect the UI layer;
  3. To put it plainly, DTO is just data, and does not contain any business logic. It is a slim type (also known as anemia) object, and can be used flexibly according to different UI requirements when used;

Application scenario

  • External service interface data model;
  • UI layer view model;
  • User's input and output parameterized model;

How to apply AutoMapper in asp.net core 3.1.x to achieve model conversion mapping?

  • Microsoft Visual Studio Enterprise 2019 版本 16.6.4;
  • .net core 3.1.7;
  • Nuget Download AutoMapper.Extensions.Microsoft.DependencyInjection -Version 8.0.1 Download

1. Create a new project [UseAutoMapperDemo] as follows:

2. Use Nuget to install dependent packages in the project;

Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection -Version 8.0.1

 Transform the created project and add two model classes [PersonA and PersonB] to the new folder [Model] to facilitate subsequent demonstrations;

2. Create AutoMapper mapping rules: Create a new class [AutoMapperProfile] in the folder [Model] to configure the mapping rules of the model;

3. Add AutoMapper in ConfigureServices of Startup:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    #region 添加AutoMapper
    services.AddAutoMapper(typeof(AutoMapperProfile));
    #endregion
}

//注意:引入对应的命名空间;
using AutoMapper;
using UseAutoMapperDemo.Model;

4. Inject your IMapper in the Controller constructor:

#region 注册IMapper
private readonly IMapper mapper;
public ValuesController(IMapper mapper)
{
   this.mapper = mapper;
}
#endregion

5. Use AutoMapper to realize object conversion;

//单对象转化
var personB = mapper.Map<PersonB>(personA);

//List集合对象转化
var personBs = mapper.Map<List<PersonB>>(personAs);

The above content is the basic use of AutoMapper in the asp.net core 3.1.x api project. For more use, please check the official website;

Demo project download: UseAutoMapperDemo.zip

Guess you like

Origin blog.csdn.net/ChaITSimpleLove/article/details/108019513