Using AutoMapper in MVC

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:

  1. public UserDto GetUserById(string id)  
  2. {  
  3.       var userDto = new UserDto();  
  4.   
  5.       using(var context = new EntityDBContext())  
  6.       {  
  7.   
  8.             var user = context.Users.Where(ID => ID.user_id == id).FirstOrDefault();  
  9.             if (user != null)  
  10.             {  
  11.                   userDto.UserId = user.user_id;  
  12.                   userDto.Name = user.user_name;  
  13.                   userDto.Address = user.user_address;  
  14.                   userDto.PhoneNo = user.user_phoneNo;  
  15.                   userDto.City = user.user_city;  
  16.             }  
  17.       }  
  18.   
  19.       return userDto;  
  20. }  
We see here that there are five properties in each class. And what we are doing here is binding the db context class to our model and then passing it to the view. Now, this looks quite simple in this case. The real problem arises when we have 25-30 fields in a record from the database and we need to repeat this same binding code all over in the project. It's awkward and painful. Copying and pasting even if it's only five properties.

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:
  1. public UserDto GetUserById(string id)  
  2. {  
  3.       var userDto = new UserDto();  
  4.   
  5.       using(var context = new EntityDBContext())  
  6.       {  
  7.   
  8.             var user = context.Users.Where(ID => ID.user_id == id).FirstOrDefault();  
  9.             Mapper.CreateMap<User,UserDto >();  
  10.             userDto = Mapper.Map<User,UserDto >(user);  
  11.       }  
  12.   
  13.       return userDto;  
  14. }  
So, we avoided the boring work of mapping properties in the code. Our CRUD operation is also simplified and organized. Just map the classes and then do Post actions. To get all the details, one change we need to make to the preceding code is that we need to add all the userDto objects to a List of userDtos.
  1. using(var context = new EntityDBContext())  
  2. {  
  3.   
  4.       var users = (from users in context.Users select users).ToList()  
  5.       Mapper.CreateMap<User,UserDto >();  
  6.       foreach(var user in users)  
  7.       {  
  8.             userDto = Mapper.Map<User,UserDto >(user);  
  9.             userDtosList.Add(userDto);  
  10.     }  
  11.   
  12. }  
We can also use the DynamicMap method instead of Map.

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:
  1. Mapper.Initialize(cfg =>  
  2. {  
  3.       cfg.CreateMap<User, UserDto>();  
  4.       cfg.CreateMap<Recipts, ReciptsDto>();  
  5.   
  6. });  
The ForMember() method

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.


Similar Articles