Getting Started With Automapper

Automapper is an object to the object mapper. By this, we can map properties of one object of one type, to the properties of another object. The automapper is widely used in the cases where DTO (Data transfer object) are used. By this, object properties can be assigned very easily from View object to DTO object and DTO object to Domain model.

What problem does the automapper solve?

In View, we have to show the personObject, while we get the PersonDTO object from the Middle layer.

  1. PersonDTO personDTO = GetPersonFromDB();  
  2. PersonView personview = new PersonView()   
  3. {  
  4.     FirstName = personDTO.FirstName,  
  5.         LastName = personDTO.LastName  
  6. }  
So, it’s a very tedious task to assign each and every property in view object. AutoMapper is the Solution for such issues.

How it works

Automapper is an object to object mapper. Object-object mapping works by transforming an input object of one type into an output object of a different type.You can find more details of automapper here.

automapper

So, the automapper sits between the source and destination object.

Now, in a console application add a nuget package automapper.

automapper

Sometime, you may face the following issue. This is basically because of the version incompatibility of nuget package manager and automapper version.

automapper

You can directly install the lower version of automapper (just for now) with package manager console, by Install-Package AutoMapper -Version 3.3.1.

automapper

Version

Now, I have created two classes:
  1. public class Person {  
  2.     public string FirstName {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string LastName {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string UID {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public DateTime DateOfBirth {  
  15.         get;  
  16.         set;  
  17.     }  
  18. }  
  19. And  
  20. public class PersonDTO {  
  21.     public string FirstName {  
  22.         get;  
  23.         set;  
  24.     }  
  25.     public string LastName {  
  26.         get;  
  27.         set;  
  28.     }  
  29.     public string UID {  
  30.         get;  
  31.         set;  
  32.     }  
  33.     public DateTime DateOfBirth {  
  34.         get;  
  35.         set;  
  36.     }  
  37. }  
We can see that both the classes have the same properties. Through automapper, we can assign property values of one object to another object. As shown in program.cs, below:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using AutoMapper;  
  7. namespace NInjectSample2 {  
  8.     class Program {  
  9.         static void Main(string[] args) {  
  10.             Mapper.CreateMap < Person, PersonDTO > ();  
  11.             Person pr = new Person() {  
  12.                 FirstName = "Vivek",  
  13.                     LastName = "Tripathi",  
  14.                     UID = "VT001"  
  15.             };  
  16.             //PersonDTO prDTO = Mapper.Map(pr, new PersonDTO());  
  17.             PersonDTO prDTO = Mapper.Map < Person, PersonDTO > (pr);  
  18.             Console.WriteLine("DTO - Name - " + prDTO.FirstName + " Lastname - " + prDTO.LastName + " - UID -" + prDTO.UID);  
  19.             Console.ReadKey();  
  20.         }  
  21.     }  
  22. }  

code
Let's see here some more complex example. Suppose, we have DTO a FullName.Property that is a combination of customer's firstname and lastname.

public string FullName { get; set; }

Then we have to change something in the Createmap.

Mapper.CreateMap<Person, PersonDTO>().ForMember(s => s.FullName, d => d.MapFrom(v => v.FirstName + " " + v.LastName));

Mapper.CreateMap

Here, we can see the changes.


Similar Articles