Using AutoMapper In MVC Project

In this article I am going to describe AutoMapper and how to use it in an MVC project.

Before starting this article I would like to give a definition of AutoMapper. AutoMapper is an object-object mapper which 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 wil 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. {    
  3.   
  4.     public string FirstName { getset; }    
  5.         
  6.     public string LastName { getset; }    
  7.         
  8.     public string Password { getset; }    
  9.        
  10.     public string Email { getset; }    
  11.     public string ImageUrl { getset; }    
  12.      
  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);  
  6.         
  7.     }  
  8. }  
Now here is my "MyProjectBLL" class where I implemented the IRegister.
  1. public class RegisterManager:IRegister    
  2. {    
  3.         
  4.     CodeXEntities _codex = new CodeXEntities();    
  5.     public bool Register(RegisterModel reg)    
  6.     {    
  7.            
  8.             bool result = false;    
  9.             tbl_Registration tbl = new tbl_Registration();    
  10.             tbl.Email = reg.Email;    
  11.             tbl.FirstName = reg.FirstName;    
  12.             tbl.LastName = reg.LastName;    
  13.             tbl.Password = reg.Password;    
  14.              
  15.             _codex.tbl_Registration.Add(tbl);    
  16.             if (_codex.SaveChanges()==1)    
  17.             {    
  18.                 return true;    
  19.             }    
  20.           else  
  21.           {  
  22.           return false;   
  23.            }  
  24.        
  25.            
  26.          }   
 

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.        
  4.     CodeXEntities _codex = new CodeXEntities();    
  5.     public bool Register(RegisterModel reg)    
  6.     {    
  7.            
  8.             bool result = false;    
  9.                
  10.             Mapper.CreateMap<RegisterModel, tbl_Registration>();  //creating map  
  11.   
  12.             var userDto = Mapper.Map<RegisterModel, tbl_Registration>(reg);  //  
  13.   
  14.   
  15.             _codex.tbl_Registration.Add(userDto);    
  16.             if (_codex.SaveChanges()==1)    
  17.             {    
  18.                 return true;    
  19.             }    
  20.             else    
  21.             {    
  22.                 return false;    
  23.             }    
  24.                 
  25.          
  26.            
  27.     }   
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: