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,
- Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection
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,
- public class Contact
- {
- public string Name { get; set; }
- public string SecurityNumber { get; set; }
- public string Address { get; set; }
- }

Kritsana YeesunkaewPosted Mar 16, 2018, 11:26 AM
Better than Automapper?