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 classes, then you can map each property one by one as shown in the 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); } } |
However, the above coding practice does not look like efficient or smart programming.
To map these 2 objects we will use Automapper.
So, to use Automapper, right-click on the project name in Visual Studio and select “Manage NuGet Packages…”
Search for Automapper and install it in your project. After the successful installation of AutoMapper, change the code in the 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 values in the destination object i.e. in the 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 the above example, we have used the same property name in both classes.
How to map different property names between objects.
Consider the below scenario.
class Employee { public string Name { get; set; } public string City { get; set; } } |
In the 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 run this code, you will get NULL
output.
Note – I have just changed the property name while printing on the 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 run your application, you are going to get the same output.
Hope you like this blog. Please comment with 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