Skip to main content

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.




Comments

Popular posts from this blog

Asp.net- Encrypt and Decrypt connection strings in web.config file

In this article I will explain how to encrypt or decrypt connectionStrings in web.config file using asp.net. If we are using applications in our internal servers with security then it’s ok if we deploy our applications in shared host environment then we have chance to arise security problems to avoid these problems asp.net 2.0 provided built in protected configuration model functionality to encrypt or decrypt few sections of web.config file those are RSAProtectedConfigurationProvider :   This is default provider and uses the RSA public key encryption algorithm to encrypt and decrypt data. DataProtectionConfgurationProvider : This provider uses windows data protection application programming interface to encrypt and decrypt the data. The encrypting and decrypting of connection strings in web.config file will do by using aspnet_regiis.exe  command line tool and  code behind . First Method : First we will do encryption and decryption using  aspnet...

CRM Automate build Solution using Powershell commands

In CRM if there is any solution movement from other enviornment like DEV,PROD,STG we use to export the solution and import the solution to the respective environment by logging into MS Dynamics CRM. This will tends to extra effort to do manually for developers or release managers. So what if release user or test user can build and deploy the Solution deployment without CRM intervention(CRM loggin in) We need to do some automate build and deployment. In this article i write about how Import/Export solution automate to the various environment without intervention of logging into MS Dynamics CRM. XRM CI Framework,this is one tool use to automate the build in CRM       Below link is for download the XRM CI Framework https://xrmciframework.codeplex.com/releases/view/125516 Download and extract the ZIP file . First HelloWorld example “WhoAmIRequest” 1.Open "Powershell" and "Run as a Administrator" 2.Navigate to Extrac...

Auto number generation using C# and SQL

Auto number generation using C# and SQL ------------------------------------------------------ Req :  Create a Autonumber for Particular Application form(Eg:Order or Quote) Approach : ------------- 1.Create a Separate autonumber configuration table which looks like below AutoNumConfig table --------------------------   AutoNumberId  AutoNumber   FormNameorFormId   -------------------------------------------    1             ORD-0002     Order or 1066    2             QUO-0002     Quote or 1067 2.Create Transaction table for Application  Form(Eg:Order/Quote) which looks like below OrderTable -------------- OrderId   OrderName  OrderAutoNum -------------------------------------------   1       Spartan      ORD-0001   2       Xamarin      ORD-0002 QuoteT...