ASP.NET CORE use AutoMapper object mapping

ASP.NET CORE use AutoMapper object mapping

1. What is AutoMapper?

AutoMapper object to the object based on the agreed mapping tools commonly used in (but are not limited to) the complex object model into DTO, generally used for ViewModel pattern and cross-service areas.

AutoMapper provides users a convenient configuration API, just use the convention to complete the automatic mapping that.

AutoMapper includes the following features: flat projection, configure authentication, lists and arrays, nested mappings, custom type conversion program, custom value conversion process, the value of the custom format program, replace the value Null

AutoMapper is a one-way mapper. This means that it does not have built-in mapping object supports write back and forth to the original source, unless the user explicitly create retroreflective after updating the mapping object. This is done by design is complete, because let DTO written back, say: domain model or something else, it will change its persistence, but it also believes that it is an anti-pattern. In this solution, the command message is a two-way mapping is often the better choice. However, in certain circumstances, one might argue for the two-way mapping, such as: very simple CRUD application. A framework that supports bi-directional mapping Glue.

2, the use AutoMapper in ASP.NET CORE

execute command to install dependencies in the package management console:

PM> Install-Package AutoMapper -Version 8.1.1
PM> Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection -Version 6.1.1

 ② use dependency injection service from the application cited

() Method call AddAutoMapper in ConfigureServices Startup class () Service:

1 public void ConfigureServices(IServiceCollection services)
2 {
3     services.AddAutoMapper();
4 }

③ create AutoMapper mapping rules

Add AutoMapperConfigs class, class inheritance 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 }

 ④ in the constructor injection IMapper

1 public class LoginController : Controller
2 {
3     private IMapper _mapper;
4 
5     public LoginController(IMapper mapper)
6     {
7         _mapper = mapper;
8     }
9 }

⑤ using object-conversion AutoMapper

Single object conversion

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 }

Conversion collection

public List <Student> GetStudentList () 
   { 
       // analog data 
       var StuList = new new List <StuInfo> () 
       { 
           new new StuInfo () = {Id . 1 , the Name = " Joe Smith " , Sex = " M " , Age = 18 is } ,
            new new StuInfo () {Id = 2 , the Name = " John Doe " , Sex = " M " , Age = . 19 },
            new new StuInfo () = {Id . 3 , the Name = " Wang Wu ", Sex = " M " , Age = 18 is }, 
       }; 
       // . Dto turn set the object set 
       var Students. _Mapper.Map = <List <Student >> (StuList);
        return Students.; 
   }

AutoMapper very powerful, support different attribute names, NULL value substitution, etc., introduced here only simple common method, AutoMapper For details, see the official website address: http://automapper.org/

Guess you like

Origin www.cnblogs.com/gygg/p/11286869.html