Professional Documents
Culture Documents
NET EF5 Automaper
NET EF5 Automaper
In this article I will explain the AutoMapper component and how to use it in an Asp.Net MVC application with few
source code samples.
About AutoMapper
AutoMapper is an external component that helps in creating a mapping between a source and destination model
types. Once the mapping is created then the source model object can be converted to a destination model object with
ease and with less cluttered code. The developers can get rid of the explicit conversions as implemented in the
following code.
AutoMapper works on a convention based mechanism, which means that the conventions specified by the
AutoMapper component need to be followed to get it working. The component can be imported to the application
through NuGet packages and more information can be found at AutoMapper.org.
Fig 1.0 shows the AutoMapper components added in the project references after the NuGet package import was
done.
1. Create a mapping between the Source and Destination model types. This should be done at the application level
and the best place would be the Global.asax page.
Flattening
This is a straight one to one mapping between the objects and the mapping is made through the property names.
AutoMapper expects the property names to be the same between the ViewModel and DomainModel.
In the GetCar action method let us use AutoMapper to convert the AutomobileDM to CarDM. The source code
example is provided below.
1. [HttpPost]
2. public ActionResult GetCar(string registrationNumber)
3. {
4. AutomobileDM automobile =
DataAccess.GetAutomobileFromDatabase(registrationNumber);
5.
6. //Create the mapping between AutomobileDM to CarVM
7. Mapper.CreateMap<AutomobileDM, CarVM>();
8.
9. //Perform the conversion and fetch the destination view model
10. CarVM car = Mapper.Map<CarVM>(automobile);
11.
12. return View("GetCar", car);
13. }
Projection Feature
Projection is the feature where you want to create custom mapping between the properties where the property name
doesn’t match or it is not a one to one mapping. In our automobile example let us move to the next step by adding the
following property to the CarVM model.
1. [HttpPost]
2. public ActionResult GetCar(string registrationNumber)
3. {
4. AutomobileDM automobile =
DataAccess.GetAutomobileFromDatabase(registrationNumber);
5.
6. //Create the mapping between AutomobileDM to CarVM
7. Mapper.CreateMap<AutomobileDM, CarVM>()
8. .ForMember(dest => dest.TotalPurchasePrice, opt => opt.MapFrom(src =>
src.Price + src.Tax));
9.
10. //Perform the conversion and fetch the destination view model
11. CarVM car = Mapper.Map<CarVM>(automobile);
12.
13. return View("GetCar", car);
14. }
Nested Models
What if the models are nested? This feature of AutoMapper is to do just that. Let us take our example to the next
level by making the domain and view model nested.
Create class InvoiceVM and add a property to CarVM with type InvoiceVM.
1. [HttpPost]
2. public ActionResult GetCar(string registrationNumber)
3. {
4. AutomobileDM automobile =
DataAccess.GetAutomobileFromDatabase(registrationNumber);
5.
6. //Create the mapping between AutomobileDM to CarVM
7. Mapper.CreateMap<AutomobileDM, CarVM>();
8. //As a next step define the mapping for the nested Invoice objects
9. Mapper.CreateMap<InvoiceDM, InvoiceVM>();
10.
11. //Perform the conversion and fetch the destination view model
12. //Note that here there is no explicit mentioning of the Invoice objects
13. CarVM car = Mapper.Map<CarVM>(automobile);
14.
15. return View("GetCar", car);
16. }
The important thing to note in all of the above examples is that only the CreateMapping call is getting altered and not
the Map method calls. So the customization will be required to be done only once in your application and the
conversion code will not require any changes to get these features implemented.
In addition to the above features there are many more, like type converters, value resolvers, Containers, inheritance
in the mapping, etc. Happy reading!