Mapping Similar Objects In ASP.NET Core 2.0

This post is about Automapper. As its name suggests, it will do some sort of mapping. Now, the question is, what sort of mapping? Well, this mapping is all about mapping the properties of two objects with each other. If you have worked on MVC, you must have come across the scenario, where the situation demands you to map properties of the model with the properties of ViewModel. Isn't it a common scenario? So, one way to achieve this is to map each and every property manually with a lot of code, which in turn become very tedious when there are many properties of an object. Another major disadvantage of this approach is it's error-prone.

Hence, Automapper came to rescue. Automapper is an object to object mapping which reduces the manual effort of mapping each property of a class with the same properties of another class. So, let’s start by writing some code and see how it works.

Step 1

First step is to add the required dependency. This can be done either by using UI or by using Nuget package console,

  1. Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection  
if you are using console to add dependency, then on successful installation of the package, the below message will be shown,

Step 2

Get ready with classes which are to be mapped. Here in this case, we have one model class and one ViewModel class as shown below,
  1. public class Contact  
  2.     {  
  3.         public string Name { getset; }  
  4.         public string SecurityNumber { getset; }  
  5.         public string Address { getset; }  
  6.     }  
  1. public class ContactViewModel  
  2.     {  
  3.         public string Name { getset; }  
  4.         public string SecurityNumber { getset; }  
  5.         public string City { getset; }  
  6.         public string State { getset; }  
  7.     }  
Step 3

Create a class which will take care of all the mappings as shown below, by inheriting the profile which is defined in Automapper namespace,
  1. public class MappingEntity:Profile  
  2.     {  
  3.         public MappingEntity()  
  4.         {  
  5.             CreateMap<ContactViewModel, Contact>();  
  6.         }  
  7.     }  
Step 4

Register this MappingEntity class in Startup class under ConfigureServices method as shown below,
  1. public void ConfigureServices(IServiceCollection services)  
  2.         {  
  3.             services.AddMvc();  
  4.             services.AddAutoMapper(x => x.AddProfile(new MappingEntity()));  
  5.         }  
Step 5

Final step is to do the changes in Controller class to accomodate this mapping.
  1. public class HomeController : Controller  
  2.    {  
  3.        private readonly IMapper _mapper;  
  4.        public HomeController(IMapper mapper)  
  5.        {  
  6.            _mapper = mapper;  
  7.        }  
  9.        public IActionResult Index(ContactViewModel vm)  
  10.        {  
  11.            var contact = _mapper.Map<Contact>(vm);  
  12.            return View();  
  13.        }  
  15.     ...  
  16.   }  
Now, as per the business requirement, the contact object declared in line number 11 can be used. That's all about automapping.
Happy learning!