Using AutoMapper In MVC Project

Before starting this article I would like to give a definition of AutoMapper. AutoMapper is an object-object mapper that allows you to solve the problem of manually mapping each property of a class with the same properties of another class.

Before AutoMapper was introduced if we wanted to assign one object property to another object property then we were following a long procedure. We had to map each property of these two different objects. Suppose in one class we have 30 properties and we want to map this with another class having 30 properties, then we would have to map this property one-by-one 30 times.

Thus for this problem, a new concept, AutoMapper, was introduced. It solves this problem using two steps.

Here I will explain how we can use AutoMapper to perform a simple insert operation. 
Create a new MVC project with the following layers. Here I have used Repository layer where all my interfaces are stored.


Here I am not explaining all the layers as you already know I have used Entity Framework with Repository pattern. In Repository Layer I have the interface for a particular operation.

Here is my UI for Registration.

And here I have declared a model as follows.
  1. public class RegisterModel    
  2. {    
  4.     public string FirstName { getset; }    
  6.     public string LastName { getset; }    
  8.     public string Password { getset; }    
  10.     public string Email { getset; }    
  11.     public string ImageUrl { getset; }    
  13. }  
Now here is where the interface for the Registration which I have defined inside the Repository layer is.
  1. namespace MyProjectRepository  
  2. {  
  3.     public interface IRegister  
  4.     {  
  5.         bool Register(RegisterModel reg);  
  7.     }  
  8. }  
Now here is my "MyProjectBLL" class where I implemented the IRegister.
  1. public class RegisterManager:IRegister      
  2. {      
  3.     CodeXEntities _codex = new CodeXEntities();      
  4.     public bool Register(RegisterModel reg)      
  5.     {        
  6.         bool result = false;      
  7.         tbl_Registration tbl = new tbl_Registration();      
  8.         tbl.Email = reg.Email;      
  9.         tbl.FirstName = reg.FirstName;      
  10.         tbl.LastName = reg.LastName;      
  11.         tbl.Password = reg.Password;      
  13.         _codex.tbl_Registration.Add(tbl);      
  14.         if (_codex.SaveChanges()==1)      
  15.         {      
  16.             return true;      
  17.         }      
  18.         else    
  19.         {    
  20.             return false;     
  21.         }     
  22.     }   
  23. } 

This is the manual way of mapping each class property one by one with another class property so it is very complicated when the property of class increases. So to avoid this we have to use Automapper.

To use AutoMapper first install NuGet. Then, install AutoMapper from the package manager console: 
  1. PM> Install-Package AutoMapper   
Or you can install directly from Nuget Package Manager as follows:


After installing this we have to use the following Namespace.
  1. using AutoMapper;
Now for using automapper there are two steps: 
  1. Create Map

    We can create a map by Mapper.createMap<sourceClass,DestinationClass>();

  2. Saving the Map Details
So here we have to use this as above image.

Now I am practically using this in this application. So now I have to modify "MyProjectBLL" class as follows to implement AutoMapper.
  1. public class RegisterManager:IRegister      
  2. {       
  3.     CodeXEntities _codex = new CodeXEntities();      
  4.     public bool Register(RegisterModel reg)      
  5.     {            
  6.         bool result = false;          
  7.         Mapper.CreateMap<RegisterModel, tbl_Registration>();  //creating map  
  8.         var userDto = Mapper.Map<RegisterModel, tbl_Registration>(reg);  //    
  9.         _codex.tbl_Registration.Add(userDto);      
  10.         if (_codex.SaveChanges()==1)      
  11.         {      
  12.             return true;      
  13.         }      
  14.         else      
  15.         {      
  16.             return false;      
  17.         }     
  18.     }  
  19. } 
Thus here we have implemented automapper. The case where we are writing a lot of code to map two class properties one by one is now handled by Automapper.

Now run the application and try to register --  it will work fine.

Thus it will work fine and register the user. So in this way we can use AutoMapper to map different properties of class.
Read more articles on ASP.NET:

Similar Articles