Sunday, June 19, 2016

MVC: Using AutoMapper

When you using 3-tiers architecture with MVC, you need to map the main model with MVC model.
you can do it by multiple ways, the best is to have centralized method to do mapping between two objects in different class.

The AutoMapper library provide this facility, you can download it via visual studio / NuGet pakage.

It is very easy and dynamic.

Example,
Suppose you have ProfileInfo class in the main model, with the following definition:




and ProfileInfo class in MVC model, which inherited the ProfileInfo class without any additional properties


In the controller, you need to map main ProfileInfo with MVC ProfileInfo after getting data from BLL, then present the MVC ProfileInfo result in the View. You should do the following:



Note, this is an example, but the best practice as mentioned is to have centralized method. So, you can put the map config in common method which will be shared between all classes.

En jo y

No comments:

Post a Comment