AutoMapper in C#
AutoMapper is utility package provided by the Microsoft net framework. Automapper provides mapping between one class object to another class object. In simple words assigning properties values of one class into another class is called automapping. You can download automapper package from nu-get package manager or package manager console.
For Example If I created two classes Person and employee as below
public class person { public string FirstName; public string LastName; public string Address; } public class Employee { public string FirstName; public string LastName; public string Address; }
Now I want to mapping(Assigning person properties values into employee properties) between them , Traditional approach to do this
static void Main(string[] args) { person pobj = new person(); pobj.FirstName = "Yogesh"; pobj.LastName = "Sharma"; pobj.Address = "Pratap nagar"; Employee eobj = new Employee(); eobj.FirstName = pobj.FirstName; eobj.LastName = pobj.LastName; eobj.Address = pobj.Address; }
This code will work fine but the cons of this approach to write lots of code.
Now see that how It can be done using automapper.
To implement this we need to install automapper utility through Microsoft Nu-Get
RightClick on Project->Select Manage NuGet Packages->Write automapper in search box ->After searching finishes ,select and install automapper.
After this process AutoMapper package will be added in references folder.
Now I am going to write code again using automapper.
static void Main(string[] args) { person pobj = new person(); pobj.FirstName = "Yogesh"; pobj.LastName = "Sharma"; pobj.Address = "Pratap nagar"; //Create AutoMapper Mapper.CreateMap<person, Employee>(); //Use created map Employee emp = Mapper.Map(pobj); }
Employee classes properties are automatically mapper because of same property name are assigned to person class but if any case when source and destination classes properties are different then we have to do properties mapping also.
Like classes structure are
public class person { public string FirstName; public string LastName; public string Address; } public class Employee { public string Fname; public string LName; public string Address; }
Then write mapping like this.
Mapper.CreateMap<person, Employee>().ForMember(dest => dest.Fname, opt => opt.MapFrom(src => src.FirstName)).ForMember(dest => dest.LName, opt => opt.MapFrom(src => src.LastName));
Complete Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
namespace AutoMapper
{
class Program
{
static void Main(string[] args)
{
person pobj = new person();
pobj.FirstName = "Yogesh";
pobj.LastName = "Sharma";
pobj.Address = "Pratap nagar";
//Create AutoMapper
Mapper.CreateMap<person, Employee>().ForMember(dest => dest.Fname, opt => opt.MapFrom(src => src.FirstName)).ForMember(dest => dest.LName, opt => opt.MapFrom(src => src.LastName));
//Use created map
Employee emp = Mapper.Map(pobj);
}
}
public class person
{
public string FirstName;
public string LastName;
public string Address;
}
public class Employee
{
public string Fname;
public string LName;
public string Address;
}
}
Download Source Code
Latest Version of AutoMapper example code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using AutoMapper; namespace ConsoleApplication10 { class Program { static void Main(string[] args) { #region Same Properties Mapping //DestClass1 obj = new DestClass1(); //Srcclass1 obj1src = new Srcclass1(); //obj1src.id = 10; //obj1src.name = "Yogesh Sharma"; //var config = new MapperConfiguration(cfg => { // // If Properties are same // cfg.CreateMap<Srcclass1, DestClass1>(); //}); //IMapper mapper = config.CreateMapper(); //obj = mapper.Map<Srcclass1, DestClass1>(obj1src); //Console.WriteLine(obj.id + " " + obj.name); //Console.ReadLine(); #endregion #region Different Properties Mapping DestClass2 obj = new DestClass2(); Srcclass1 obj1src = new Srcclass1(); obj1src.id = 10; obj1src.name = "Yogesh Sharma"; var config = new MapperConfiguration(cfg => { cfg.CreateMap<Srcclass1, DestClass2>(). ForMember(o => o.id1, b => b.MapFrom(b1 => b1.id)).ForMember(o=>o.name1 , b => b.MapFrom(b1 => b1.name)); }); IMapper mapper = config.CreateMapper(); obj = mapper.Map<Srcclass1, DestClass2>(obj1src); Console.WriteLine(obj.id1 + " " + obj.name1); Console.ReadLine(); #endregion } } class Srcclass1 { public int id; public string name; } class DestClass1 { public int id; public string name; } class DestClass2 { public int id1; public string name1; } }