What is Automapper and what is purpose of using this in ASP .Net MVC? [Resolved]

Posted by Kumarkrishna184 under ASP.NET MVC on 12/28/2015 | Points: 10 | Views : 1023 | Status : [Member] | Replies : 2
What is Automapper and what is purpose of using this in ASP .Net MVC?

Thanks and Regards,
Krishna Kumar



Responses

Posted by: Sheonarayan on: 12/28/2015 [Administrator] HonoraryPlatinum | Points: 50

Up
0
Down

Resolved
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

Posted by: Kumarkrishna184 on: 12/28/2015 [Member] Starter | Points: 25

Up
0
Down
Thanks Alot Sheo Sir, Now I am very clear about Automapper....

Thanks and Regards,
Krishna Kumar

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response