Using AutoMapper in C#

AutoMapper is used to map data from object to objects.
 
In a real project the entity layers always deal with the communication from services or Data Layer.To show the data in the application we need separate class as ViewModel or Model class . UI Layers may or may not be synced with the entities. So to Map entities to model or viewModel we need AutoMapper.
 
Let us see how to do that,
 
First we need to get the AutoMapper from the Nuget package as shown below.


Let us say we have a class at entity layer as below,

We need to create a class i.e Employee.cs at entity layer as below,

  1. using System;  
  2.   
  3. namespace DemoEntities  
  4. {  
  5.    /// <summary>  
  6.    /// Entity class which is used to keep all the information related to  business domain .  
  7.    /// </summary>  
  8.     public class Employee  
  9.     {  
  10.         public int EmployeeId { getset; }  
  11.         public string EmployeeFName { getset; }  
  12.         public string EmployeeLName { getset; }  
  13.         public string Address { getset; }  
  14.         public string City { getset; }  
  15.         public string State { getset; }  
  16.         public string Zip { getset; }  
  17.         public DateTime? DateOfJoining { getset; }  
  18.   
  19.     }  
  20. }  

We have another class called User class at UI Layer.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace DemoAutomapper.Models  
  7. {  
  8.   
  9.     /// <summary>  
  10.     /// Entity class which is used to keep all the information related to  specific application   
  11.     /// All fields of entity may or may not be present .  
  12.     /// </summary>  
  13.     public class User  
  14.     {  
  15.         public int Userid { getset; }  
  16.         public string UserFName { getset; }  
  17.         public string UserLName { getset; }  
  18.         public string Address { getset; }  
  19.         public string City { getset; }  
  20.         public string State { getset; }  
  21.         public string Zip { getset; }  
  22.         public DateTime? DateOfJoining { getset; }  
  23.          
  24.     }  
  25. }  

We want to map Employee class to a User class, by using Automapper we can map as below,

  1. Employee objEmployee = new Employee  
  2.          {  
  3.              EmployeeId = 1001,  
  4.              EmployeeFName = "Pradeep",  
  5.              EmployeeLName = "Sahoo",  
  6.              Address = "KRPURAM",  
  7.              City = "BANGALORE",  
  8.              State = "KA",  
  9.              Zip = "560049",  
  10.              DateOfJoining = DateTime.Now,  
  11.          };  
  12.   
  13.   
  14.          Mapper.CreateMap<Employee, User>(); //Creates the map and all fields are copied if properties are same   
  15.   
  16.   
  17.          If properties are different we need to map fields of employee to that of user as below.  
  18.   
  19.          AutoMapper.Mapper.CreateMap<Employee, User>()  
  20.          .ForMember(o => o.Userid, b => b.MapFrom(z => z.EmployeeId))  
  21.          .ForMember(o => o.UserFName, b => b.MapFrom(z => z.EmployeeFName))  
  22.          .ForMember(o => o.UserLName, b => b.MapFrom(z => z.EmployeeLName));  
  23.   
  24.          User objuser = Mapper.Map<Employee, User>(objEmployee);  

Let us see the output,

 

We saw how to map Entities to Model class using AutoMapper. Thanks for reading.