How To Implement AutoMapper In ASP.NET Core MVC Application

In this demo, I will show how to utilize the Automapper library efficiently. Automapper makes our lives easy with minimal steps. In a nutshell, AutoMapper is an object-object mapper. It transforms the input object of one type into an output object of another type.

Requirements

  • Visual Studio 2017.
  • Auto Mapper NuGet Package
  • Auto Mapper Dependency Injection Package

In this example, I’ve taken two classes, Employee and EmployeeModel.

  1. namespace ASPNETCORE_AUTOMAPPER.Models {  
  2.     public class Employee {  
  3.         public int Id {  
  4.             get;  
  5.             set;  
  6.         }  
  7.         public string Name {  
  8.             get;  
  9.             set;  
  10.         }  
  11.         public string Designation {  
  12.             get;  
  13.             set;  
  14.         }  
  15.         public string City {  
  16.             get;  
  17.             set;  
  18.         }  
  19.         public string State {  
  20.             get;  
  21.             set;  
  22.         }  
  23.     }  
  24. }  
  25. namespace ASPNETCORE_AUTOMAPPER.Models {  
  26.     public class EmployeeModel {  
  27.         public int Id {  
  28.             get;  
  29.             set;  
  30.         }  
  31.         public string Name {  
  32.             get;  
  33.             set;  
  34.         }  
  35.         public string Designation {  
  36.             get;  
  37.             set;  
  38.         }  
  39.         public Address Address {  
  40.             get;  
  41.             set;  
  42.         }  
  43.     }  
  44.     public class Address {  
  45.         public string City {  
  46.             get;  
  47.             set;  
  48.         }  
  49.         public string State {  
  50.             get;  
  51.             set;  
  52.         }  
  53.     }  
  54. }  

We are getting Employee object from the end user and trying to assign it to EmployeeModel with each property like below, which is a tedious job and in real time scenarios, we may have plenty of properties as well as complex types. So this is an actual problem.

  1. [HttpPost]  
  2. public EmployeeModel Post([FromBody] Employee employee) {  
  3.     EmployeeModel empmodel = new EmployeeModel();  
  4.     empmodel.Id = employee.Id;  
  5.     empmodel.Name = employee.Name;  
  6.     empmodel.Designation = employee.Designation;  
  7.     empmodel.Address = new Address() {  
  8.         City = employee.City, State = employee.State  
  9.     };  
  10.     return empmodel;  
  11. }  

To overcome this situation, we have a library called AutoMapper.

Incorporate this library into your application by following the below steps.

Open Visual Studio and Click on File - New Project and select ASP.NET CORE WEB APPLICATION,

How To Implement AutoMapper In ASP.NET Core MVC Application

Click on Ok and you’ll get the below window where you have to select WebApp (MVC).

How To Implement AutoMapper In ASP.NET Core MVC Application

As soon as you click on the Ok button your application is ready.

Now, the actual auto mapper should take place. For that, we need to add NuGet reference to the solution. Make sure we have to add two references to solution

  • Add Main AutoMapper Package to the solution,

    How To Implement AutoMapper In ASP.NET Core MVC Application

  • Now, add the Auto mapper dependency Injection Package,

    How To Implement AutoMapper In ASP.NET Core MVC Application

  • Now, call AddAutoMapper from StartUp.cs file as shown below,
    1. public void ConfigureServices(IServiceCollection services) {  
    2.     services.AddMvc();  
    3.     services.AddAutoMapper();  
    4. }  
  • Now, create the MappingProfile.cs file under the root project and write the below snippet
    1. public class MappingProfile: Profile {  
    2.     public MappingProfile() {  
    3.         CreateMap < Employee, EmployeeModel > ()  
    4.     }  
    5. }  
  • Here CreateMap method is used to map data between Employee and EmployeeModel.

If you observe here we called ForMember method, which is used when we have different datatypes in source and destination classes.

Employee Class should be like this,

  1. namespace ASPNETCORE_AUTOMAPPER.Models {  
  2.     public class Employee {  
  3.         public int Id {  
  4.             get;  
  5.             set;  
  6.         }  
  7.         public string Name {  
  8.             get;  
  9.             set;  
  10.         }  
  11.         public string Designation {  
  12.             get;  
  13.             set;  
  14.         }  
  15.         public string City {  
  16.             get;  
  17.             set;  
  18.         }  
  19.         public string State {  
  20.             get;  
  21.             set;  
  22.         }  
  23.     }  
  24. }  

EmployeeModel.cs should be like this,

  1. namespace ASPNETCORE_AUTOMAPPER.Models {  
  2.     public class EmployeeModel {  
  3.         public int Id {  
  4.             get;  
  5.             set;  
  6.         }  
  7.         public string Name {  
  8.             get;  
  9.             set;  
  10.         }  
  11.         public string Designation {  
  12.             get;  
  13.             set;  
  14.         }  
  15.         public Address Address {  
  16.             get;  
  17.             set;  
  18.         }  
  19.     }  
  20.     public class Address {  
  21.         public string City {  
  22.             get;  
  23.             set;  
  24.         }  
  25.         public string State {  
  26.             get;  
  27.             set;  
  28.         }  
  29.     }  
  30. }  

In Employee.cs file having City and State properties but in EmployeeModel.cs we have Address type. So if we try to map these two models we may end up missing type configuration error. So to overcome that issue we have to use ForMember method which tells mapper what properties it should map for that particular Address field. So we have to tweak the MappingProfile.cs file like below:

  1. public class MappingProfile: Profile {  
  2.     public MappingProfile() {  
  3.         CreateMap < Employee, EmployeeModel > ().ForMember(dest => dest.Address, opts => opts.MapFrom(src => new Address {  
  4.             City = src.City, State = src.State  
  5.         }));  
  6.     }  
  7. }  

So the next step is we have to hook this up from our controller;  just follow the below snippet

  1. namespace ASPNETCORE_AUTOMAPPER.Controllers {  
  2.     public class EmployeeController: Controller {  
  3.         private readonly IMapper _mapper;  
  4.         public EmployeeController(IMapper mapper) {  
  5.             _mapper = mapper;  
  6.         }  
  7.         public IActionResult Index() {  
  8.                 return View();  
  9.             }  
  10.             [HttpPost]  
  11.         public EmployeeModel Post([FromBody] Employee employee) {  
  12.             EmployeeModel empmodel = new EmployeeModel();  
  13.             empmodel = _mapper.Map < Employee, EmployeeModel > (employee);  
  14.             return empmodel;  
  15.         }  
  16.     }  
  17. }  

Here we have injected IMapper to EmployeeController and performed mapping operation between Employee and EmployeeModel

If you pass data to Employee object,  it will directly map to EmployeeModel object with the help of Mapper.

Now if you observe, EmployeeModel is filled in with all the property data with the help of Mapper. This is the actual beauty of AutoMapper.

So if you come across a requirement to map data from your DTOs to Domain object choose Automapper and it will do all your work with less code.