Search This Blog

Monday, June 22, 2015

Auto Mapper in MVC:

Auto Mapper 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 re usability 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 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:
//Return UserDTO Class
public UserDto GetUserById(string id)
{
//Create new Object for DTO class
var userDto = new UserDto();
//Create New Object For Entity(DB class)
using(var context = new EntityDBContext())
{
//Get the single row from DB using LINQ query
//It always use using statement for best practice to dispose object automatically
var user = context.Users.Where(ID => ID.user_id == id).FirstOrDefaul();
if (user != null)
{
//Fill DB object to UserDTO Class
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;
}   

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.

What is 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 configuration is needed to map two types." Therefore, it provides the solution for our mapping issue.
   

   Firstly install the NuGet Package Manager in your Visual Studio IDE. Once done, go to:
     Tools -> Library Packet Manager -> Packet manager Console
Then in the console window opened at the bottom of Visual Studio, type:
                  PM> Install-Package AutoMapper
Press Enter, this will install AutoMapper and the next time you open an MVC application in Visual Studio, it will automatically add a DLL reference to the project.

Use of Automapper:

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:
There are two steps to use this Automapper

Step 1 :

//UserDTO-DTO Object Class//User-Model Class to bind the View//Fill UserDTO to User model

                           Mapper.CreateMap<User, UserDto>();
Step 2 :
                          Mapper.Map<User,UserDto >(user);
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();
//Create Map for DTO and Model Object
Mapper.CreateMap<User,UserDto >();
//Fill the DTO object to Model Object and reduce the lot of code.
userDto = Mapper.Map<User,UserDto >(user);
}
return userDto;
}
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.
   

ForMember() and MapFrom() in AutoMapper

Two important functions in AutoMapper play an important role in object mapping. Suppose our model/viewmodel class has a property FullName, and from the DTO we want to add the First Name and Last Name of the user to make it a full name and bind it to the model. For these kinds of scenarios ForMember() andMapFrom() come in handy. See below code:
   Mapper.CreateMap<LearningMVC.User, LearningMVC.Models.User>().ForMember(emp => emp.Fullname,
map => map.MapFrom(p => p.FirstName + " " + p.LastName));

Here we are saying that ForMember FullName in our model class maps properties from FirstName and LastName of User DTO.

The code itself is self-explanatory. This kind of mapping is also called Custom Mapping.




No comments:

Post a Comment

Note: Only a member of this blog may post a comment.