Before starting this article I would like to give a definition of AutoMapper. AutoMapper is an object-object mapper which allows you to solve the problem of manually mapping each property of a class with the same properties of another class.
Before AutoMapper was introduced if we wanted to assign one object property to another object property then we were following a long procedure. We had to map each property of these two different objects. Suppose in one class we have 30 properties and we want to map this with another class having 30 properties, then we would have to map this property one-by-one 30 times.
Thus for this problem, a new concept, AutoMapper, was introduced. It solves this problem using two steps.
Here I wil explain how we can use AutoMapper to perform a simple insert operation.
Create a new
MVC project with the following layers. Here I have used Repository layer where all my interfaces are stored.
Here I am not explaining all the layers as you already know I have used Entity Framework with Repository pattern. In Repository Layer I have the interface for a particular operation.
Here is my UI for Registration.
And here I have declared a model as follows.
- public class RegisterModel
- {
-
- public string FirstName { get; set; }
-
- public string LastName { get; set; }
-
- public string Password { get; set; }
-
- public string Email { get; set; }
- public string ImageUrl { get; set; }
-
- }
Now here is where the interface for the Registration which I have defined inside the Repository layer is.
- namespace MyProjectRepository
- {
- public interface IRegister
- {
- bool Register(RegisterModel reg);
-
- }
- }
Now here is my "
MyProjectBLL" class where I implemented the IRegister.
- public class RegisterManager:IRegister
- {
-
- CodeXEntities _codex = new CodeXEntities();
- public bool Register(RegisterModel reg)
- {
-
- bool result = false;
- tbl_Registration tbl = new tbl_Registration();
- tbl.Email = reg.Email;
- tbl.FirstName = reg.FirstName;
- tbl.LastName = reg.LastName;
- tbl.Password = reg.Password;
-
- _codex.tbl_Registration.Add(tbl);
- if (_codex.SaveChanges()==1)
- {
- return true;
- }
- else
- {
- return false;
- }
-
-
- }
This is the manual way of mapping each class property one by one with another class property so it is very complicated when the property of class increases. So to avoid this we have to use Automapper.
To use AutoMapper first
install NuGet. Then, install AutoMapper from the package manager console:
- PM> Install-Package AutoMapper
Or you can install directly from Nuget Package Manager as follows:
After installing this we have to use the following Namespace.
Now for using automapper there are two steps:
- Create Map
We can create a map by Mapper.createMap<sourceClass,DestinationClass>();
- Saving the Map Details
So here we have to use this as above image.
Now I am practically using this in this application. So now I have to modify "
MyProjectBLL" class as follows to implement AutoMapper.
- public class RegisterManager:IRegister
- {
-
- CodeXEntities _codex = new CodeXEntities();
- public bool Register(RegisterModel reg)
- {
-
- bool result = false;
-
- Mapper.CreateMap<RegisterModel, tbl_Registration>();
-
- var userDto = Mapper.Map<RegisterModel, tbl_Registration>(reg);
-
-
- _codex.tbl_Registration.Add(userDto);
- if (_codex.SaveChanges()==1)
- {
- return true;
- }
- else
- {
- return false;
- }
-
-
-
- }
Thus here we have implemented automapper. The case where we are writing a lot of code to map two class properties one by one is now handled by Automapper.
Now run the application and try to register -- it will work fine.
Thus it will work fine and register the user. So in this way we can use AutoMapper to map different properties of class.
Read more articles on ASP.NET: