Click here to Skip to main content
15,885,167 members
Articles / Programming Languages / C#

What is Automapper?

Rate me:
Please Sign up or sign in to vote.
4.72/5 (25 votes)
28 Apr 2015CPOL2 min read 46.2K   41   5
This post explains the need of Automapper in C# and MVC projects.

Contents

What is Automapper ?

Automapper is a simple reusable component which helps you to copy data from object type to other. If you wish to get automapper, read this.

For example, below is a simple employee class loaded with some dummy data.

C#
class Employee 
{
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public decimal Salary { get; set; }
}
// Loaded with some data
Employee emp = new Employee();
emp.FirstName = "Shiv";
emp.LastName = "Koirala";
emp.Salary = 100;

Now let’s say we want to load the above employee data into a “Person” class object.

C#
class Person
{
        public string FirstName { get; set; }
        public string LastName { get; set; }
}

So normally, developers end up writing mapping code as shown in the below snippet.

C#
Person per = new Person();
per.FirstName = emp.FirstName;
per.LastName = emp.LastName;

At first glance, this does not look much of a problem but wait…think, what if you want to use this mapping code again and again.

So as a best practice, you would like to centralize this mapping code and reuse this mapping code again and again. This is where our super hero “AutoMapper” comes to the rescue. “Automapper” sits in between both the objects like a bridge and maps the property data of both objects.

Image 1

Using Automapper is a two-step process:

  • Create the Map.
  • Use the Map so that objects can communicate.
C#
Mapper.CreateMap<Employee, Person>(); // Create Map
Person per =  Mapper.Map<Employee, Person>(emp); // Use the map

I have not specified mapping. How does it work?

In the above example property names of both classes are same so the mapping happens automatically.

How can we map different property names in Automapper?

If the classes have different property names, then we need to use “ForMember” function to specify the mapping.

Mapper.CreateMap<Employee, Person>()
.ForMember(dest => dest.FName , opt => opt.MapFrom(src => src.FirstName))
.ForMember(dest => dest.LName , opt => opt.MapFrom(src => src.LastName));

Can you give some real time scenarios of the use of Automapper?

  • When you are moving data from ViewModel to Model in projects like MVC.
  • When you are moving data from DTO to Model or entity objects and vice versa.

From where to get AutoMapper?

The easiest way is to use nugget. In case you are new to nuget, see this YouTube video.

Image 2

Or you can download the component form their main site at http://automapper.org/.

Also, see following video on use of C# Automapper:

Image 3

For Further reading do watch  the below interview preparation videos and step by step video series.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect https://www.questpond.com
India India

Comments and Discussions

 
QuestionUseful Pin
Cheung Tat Ming3-May-15 4:36
Cheung Tat Ming3-May-15 4:36 
GeneralNice Article Pin
Santhakumar Munuswamy @ Chennai1-May-15 4:50
professionalSanthakumar Munuswamy @ Chennai1-May-15 4:50 
QuestionHow to map the List? Pin
VijayRana30-Apr-15 5:39
professionalVijayRana30-Apr-15 5:39 
QuestionIs that syntax correct? Pin
rhyous29-Apr-15 15:59
rhyous29-Apr-15 15:59 
GeneralMy vote of 4 Pin
cjb11028-Apr-15 20:54
cjb11028-Apr-15 20:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.