Introduction
When we code for a realistic actual environment, we encounter many challenges to refractor our code for a better understanding and to easily identify the bugs. We mainly focus on reusability and try to move as much code as possible to a common method so that development time and maintenance costs are reduced.
In this article, I will try to cover a new concept in MVC: AutoMapper. AutoMapper is used to reduce the complexity we find when binding the model and communicating with entities.
The Real Problem
We often interact with our database entities and bind our models with it. What we end up with is somewhat like this:
- public UserDto GetUserById(string id)
- {
- var userDto = new UserDto();
- using(var context = new EntityDBContext())
- {
- var user = context.Users.Where(ID => ID.user_id == id).FirstOrDefault();
- if (user != null)
- {
- userDto.UserId = user.user_id;
- userDto.Name = user.user_name;
- userDto.Address = user.user_address;
- userDto.PhoneNo = user.user_phoneNo;
- userDto.City = user.user_city;
- }
- }
- return userDto;
- }
More complexity arises when we have a separate ViewModel for each view (which is typically used in current MVC across the companies). Now you have double the amount of work, first you need to bind your model and then again you need to bind it to the ViewModel. Think of how tedious that job would be.
To overcome this tedious situation AutoMapper is introduced. It not only reduces the effort but it also limits the execution time that has been taken by such a large number of lines to execute.
About AutoMapper
AutoMapper is an open source library provided by GitHub. The Codeplex page says about AutoMapper: “AutoMapper is an object-object mapper. Object-object mapping works by transforming an input object of one type into an output object of a different type. What makes AutoMapper interesting is that it provides some interesting conventions to take the dirty work out of figuring out how to map type A to type B. As long as type B follows AutoMapper's established conventions, almost zero configurations is needed to map two types”. As per the definition it solves our issue. You can get the AutoMapper from the following website: AutoMapper.
You can also see the samples and documentations.
Use
Now we will see where AutoMapper fits in our problem. Well, AutoMapper provides a CreateMap<T1,T2> method that provides mapping between two classes. So, the entire code can be replaced by the following single line:
Mapper.CreateMap<User, UserDto>()
Where User is a DBContext class type and UserDto is our DTO class to map.
Finally to map all the data we need to use the Map<T1,T2>() method of the AutoMapper class. So the final code will look like this:
- public UserDto GetUserById(string id)
- {
- var userDto = new UserDto();
- using(var context = new EntityDBContext())
- {
- var user = context.Users.Where(ID => ID.user_id == id).FirstOrDefault();
- Mapper.CreateMap<User,UserDto >();
- userDto = Mapper.Map<User,UserDto >(user);
- }
- return userDto;
- }
- using(var context = new EntityDBContext())
- {
- var users = (from users in context.Users select users).ToList()
- Mapper.CreateMap<User,UserDto >();
- foreach(var user in users)
- {
- userDto = Mapper.Map<User,UserDto >(user);
- userDtosList.Add(userDto);
- }
- }
Points to remember
To make the mapping work the property names of the two classes must be the same. Also, the datatype should match, that means if type A has a property called long? a then type B also should have long? a. Otherwise the AutoMapper Map will fail and throw an exception. To check whether your mapping is okay we can use the following:
Mapper.AssertConfigurationIsValid();
If the Dto and DBContext classes have a collection as properties then we need to map those collections first and then we will be able to map the parents. So there will be two CreateMap methods. Better to use it in the Initialize method as follows:
- Mapper.Initialize(cfg =>
- {
- cfg.CreateMap<User, UserDto>();
- cfg.CreateMap<Recipts, ReciptsDto>();
- });
Another important and useful method the AutoMapper provides is a ForMember() method. Suppose our dbContext class has two properties called FirstName and LastName whereas our model/viewmodel class has a property called FullName. Then we don't need to create two extra properties in our model/viewmodel. We can use the ForMember() method for the mapping.
Mapper.CreateMap<User,UserDto>().ForMember(user => user.Fullname,
map => map.MapFrom(p => p.FirstName + " " + p.LastName));
The Ignore() method
This method is useful when we have extra properties inside our destination object. This scenario is useful in ViewModel cases where we usually have extra properties. Then we can use the following:
Mapper.CreateMap<User,UserDto >()
.ForMember(dt=>dt.SessionId,options=>options.Ignore())
.ForMember(dt=>dt.PROGRAM_NAME,options=>options.Ignore());
Conclusion
By using AutoMapper, our life is much easier and it's really helpful. I suggest that developers use it if the company permits. I have provided a sample, you can have a look and also you can go the AutoMapper site and read the documentation and can download the sample.

Nurullo SulaymonovPosted Dec 22, 2019, 11:48 PM
Many Thanks bro.It was really helpful
Imon MollaPosted Mar 18, 2019, 6:22 PM
Thanks bro
Rajendra KhandekarPosted May 14, 2018, 8:44 AM
GOOD.... good work!
Dennis ThomasPosted Mar 5, 2018, 6:48 AM
Nice article.... Keep up the good work!
Dushyant PatelPosted Jun 14, 2017, 1:58 AM
Good Article. It will really helpful for beginner
Ganesh MohitePosted Jan 20, 2017, 2:40 AM
Awesome Awesome Awesome..AutoMapper
S TomPosted Mar 27, 2016, 9:23 AM
Article is good, but one suggestion: CreateMap has to be created once. You should not create it in every request, check this post: http://stackoverflow.com/a/16477595/2218697
jayesh kannappanPosted Oct 15, 2015, 7:17 AM
a gud article
FirstName LastNamePosted Jan 2, 2015, 7:44 AM
1] The article does not match the download file.
Ramesh MaruthiPosted Oct 28, 2014, 10:16 AM
Awesome Article Arunava :)
Guest UserPosted Oct 28, 2014, 4:42 AM
i liked your article. good one!