
Automapper is a third party component that you can install in your ASP.NET project with the help of NuGet Packages.
It helps to automatically map the data of one object to another. Eg.
Person p = new Person();
p.FirstName = "Sheo";
p.LastName = "Narayan";
If we have to copy above data to Employee object, we need to do following.
Employee e = new Employee();
e.FirstName = p.FirstName;
e.LastName = p.LastNeme;
This is good if we have to do it once but if we have to do this repetitively, we need to duplicate above code multiple times. To avoid this, we can use Automapper. Install the Automapper from NuGet Packages and then write following code just after setting the value of Person object.
Mapper.CreateMap<Person, Employee>(); // create map between Person and Employee
Employee e = Mapper.Map<Employee>(p); // map the matching properties of Person object to Employee
There might be scenario, where you may have different property names into both objects, in that case you can select what property of source should be mapped with what property of target. For more details on this, watch this video
http://www.dotnetfunda.com/videos/show/418/what-is-the-use-of-csharp-automapper Specific to MVC, it can be used to copy the properties of Model to ViewModel or vice-versa.
Hope this helps.
Thanks
Regards,
Sheo Narayan
http://www.dotnetfunda.com
Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator