AUTOMAPPER IN MVC

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:

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.

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:

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.

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:

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.

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.