AutoMapper is used to map data between two objects. In this blog, we will see how to use AutoMapper in our application.
Let’s demonstrate the use of Automapper by an example.
Suppose we have a class called Employee with 2 properties in it – Name and City
class Employee { public string Name { get; set; } public string City { get; set; } } |
We have another class called Member with 3 properties – Name, City, MemberType.
class Member { public string Name { get; set; } public string City { get; set; } public string MemberType { get; set; } } |
Now, if we want to map the objects of Employee and Member class, then you can map each property one by one as shown in below code.
class Program { static void Main(string[] args) { Employee employee = new Employee(); employee.Name = “Ram”; employee.City = “Delhi”; Member member = new Member(); member.Name = employee.Name; member.City = employee.City; Console.WriteLine(member.Name); Console.WriteLine(member.City); } } |
But the above coding practice does not look efficient or smart programming.
To map these 2 objects we will use Automapper.
So, in order to use Automapper right click on project name in Visual Studio and select “Manage NuGet Packages…”
Search for Automapper and install it in your project. Post successful installation of AutoMapper, change the code in main method as shown below.
class Program { static void Main(string[] args) { var config = new MapperConfiguration(am => { am.CreateMap<Employee, Member>(); }); IMapper mapper = config.CreateMapper(); var source = new Employee(); source.Name = “Ram”; source.City = “Delhi”; var dest = mapper.Map<Employee, Member>(source); Console.WriteLine(dest.Name); Console.WriteLine(dest.City); Console.ReadLine(); } } |
Once you run the application, you will get Name and City value in destination object i.e. in Member class object.
Note: I have used Visual Studio 2017 and Automapper version 7.0.1. Before AutoMapper 4.2 there was a method called CreateMap. But this method was removed from version 5.0 onwards.
In above example, we have used same property name in both the classes.
How to map different properties name between objects.
Consider below scenario.
class Employee { public string Name { get; set; } public string City { get; set; } } |
In Member class, Name is now FullName and City is changed to Location.
class Member { public string FullName { get; set; } public string Location { get; set; } public string MemberType { get; set; } } |
Now, if you will run this code, you will get NULL output.
Note – I have just changed the property name while printing on console. But that is not working.
static void Main(string[] args) { var config = new MapperConfiguration(am => { am.CreateMap<Employee, Member>(); }); IMapper mapper = config.CreateMapper(); var source = new Employee(); source.Name = “Ram”; source.City = “Delhi”; var dest = mapper.Map<Employee, Member>(source); Console.WriteLine(dest.FullName); Console.WriteLine(dest.Location); Console.ReadLine(); } |
So, to map different properties name we have to use ForMember.
var config = new MapperConfiguration(am => { am.CreateMap<Employee, Member>().ForMember(destination => destination.FullName, src => src.MapFrom(p => p.Name)) .ForMember(destination => destination.Location, src => src.MapFrom(p => p.City)); }); |
Complete code –
class Program { static void Main(string[] args) { var config = new MapperConfiguration(am => { am.CreateMap<Employee, Member>().ForMember(destination => destination.FullName, src => src.MapFrom(p => p.Name)) .ForMember(destination => destination.Location, src => src.MapFrom(p => p.City)); }); IMapper mapper = config.CreateMapper(); var source = new Employee(); source.Name = “Ram”; source.City = “Delhi”; var dest = mapper.Map<Employee, Member>(source); Console.WriteLine(dest.FullName); Console.WriteLine(dest.Location); Console.ReadLine(); } } |
Now if you will run your application, you are going to get the same output.
Hope you like this blog. Please comment your feedback below and share this blog.
You may like other blogs –
MVC Tutorial
Web API Tutorial
Is Angular JS different from Angular?
Interview Questions and Answers Series –
MVC Interview Questions and Answers
Web API interview questions and answers