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.
- PersonDTO personDTO = GetPersonFromDB();
- PersonView personview = new PersonView()
- {
- FirstName = personDTO.FirstName,
- LastName = personDTO.LastName
- }
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.
So, the automapper sits between the source and destination object.
Now, in a console application add a nuget package automapper.
Sometime, you may face the following issue. This is basically because of the version incompatibility of nuget package manager and automapper version.
You can directly install the lower version of automapper (just for now) with package manager console, by Install-Package AutoMapper -Version 3.3.1.
Now, I have created two classes:
- public class Person {
- public string FirstName {
- get;
- set;
- }
- public string LastName {
- get;
- set;
- }
- public string UID {
- get;
- set;
- }
- public DateTime DateOfBirth {
- get;
- set;
- }
- }
- And
- public class PersonDTO {
- public string FirstName {
- get;
- set;
- }
- public string LastName {
- get;
- set;
- }
- public string UID {
- get;
- set;
- }
- public DateTime DateOfBirth {
- get;
- set;
- }
- }
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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using AutoMapper;
- namespace NInjectSample2 {
- class Program {
- static void Main(string[] args) {
- Mapper.CreateMap < Person, PersonDTO > ();
- Person pr = new Person() {
- FirstName = "Vivek",
- LastName = "Tripathi",
- UID = "VT001"
- };
-
- PersonDTO prDTO = Mapper.Map < Person, PersonDTO > (pr);
- Console.WriteLine("DTO - Name - " + prDTO.FirstName + " Lastname - " + prDTO.LastName + " - UID -" + prDTO.UID);
- Console.ReadKey();
- }
- }
- }
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)); Here, we can see the changes.