AutoMapper in C#

By | July 26, 2017

AutoMapper is an object-object mapper which allows you to solve the problem of manually mapping each property of a class with the same properties of another class.

AutoMapper saves you the tedious effort of having to manually map one or more properties of such incompatible types.

Before AutoMapper was introduced if we wanted to assign one object property to another object property then we were following a long procedure. We had to map each property of these two different objects. Suppose in one class we have 30 properties and we want to map this with another class having 30 properties, then we would have to map this property one-by-one 30 times. Thus for this problem, a new concept, AutoMapper, was introduced.

How to use Automapper:

Install AutoMapper from the Nuget package manager console:

PM> Install-Package AutoMapper

or

install the latest version 6.1 form Nuget as below. In latest version, we also need to write code for configurations as shown an example at last.

Namespace :

using AutoMapper;

Now i have two classes Employee and Users class as below.

Here we will map Employee class to the user class. For example: In below code we have Employee class which holds the desire result and we need to return Users type. This time we will use automapper for mapping properties instead of mapping each property every time as below code.

 Employee result = context.Employees.Find(id);
 Mapper.CreateMap<Employee, Users>();
 Users user= Mapper.Map<Employee, Users>(result);

In above code, users class  fullname property will be null as below image because it is not available in employee class. All unmapped properties remains null.

ForMember:

ForMember configures members of the target type.

Suppose, we have a situation to map two properties Fname and Lname of employee class to User class Fullname property. That time we will use ForMember as below code example.

 Employee result = context.Employees.Find(id);
 Mapper.CreateMap<Employee, Users>().ForMember(users => users.FullName,
 map => map.MapFrom(emp => emp.Fname + " " + emp.Lname));
 Users user = Mapper.Map<Employee, Users>(result);

 

In above code, users class  fullname property will be the “Fname + _ + Lname” of employee class property as you can see in below image.

 

In above image, FullName property has a value of Fname and Lname property. It is done by using the ForMember function.

 

In this tutorial, We have used Automapper version 3.3. You can download the Library DLL from here.
If you will use the latest version 6.1 then you shuold configure the Mapper as below code.

  //Add this code for configuration
 var config = new MapperConfiguration(cfg =>
 {
 cfg.CreateMap<Employee, Users>();
 });
 config.AssertConfigurationIsValid();
 //Add this code for configuration
 
 var mapper = config.CreateMapper();
 Employee result = context.Employees.Find(id);
 Users user = mapper.Map<Employee, Users>(result);

I hope you will enjoy the use of Automapper in C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Like it? Share it