Project the Mappings once, use them a gazillion number of times.....
As discussed in previous blogs, at Incredible Web we make extensive use of the Multi Tier Architecture in our web sites. We always aim for the best standards when it comes to mantainability, flexibility and scalability thus we're always on the lookout for the right tools to help us achieve our high standards. A very useful, simple tool we came across when we were designing our own multi tier architecture was the 'AutoMapper'.
What is the AutoMapper?
As developers we've all come across situations where we have to map data from one object to another more/less complex object. Most of the times the fields are related or are infact the representation of eachother within different entities for example PersonData and PersonProfile where the PersonData object is the object retrieved from the database conatining all the information found in the Person table and the PersonProfile is the actual Object or ViewModel used in the View in order to present the data. The AutoMapper can facilitate the mapping process form one object to another for you by using some simple common conventions in order to figure out which source field needs to be mapped to which object field.
How does it do it?
AutoMapper uses a 'convention-based matching algorithm' in order to identify which field in the source object needs to be mapped to which field in the destination object. If some particular mappings need to be customized, the API allows you to customize the projections in order to suit your particular needs. Once you create the customized Map you want, the AutoMapper will look into its own configurations for that particular source and destination and use the custom mapping to project the source object to the destination object.
Why and Where should I use it?
In our Multitier Architecture, we get Data Models from the bottom Layer in the Data Access Layer (which we discussed into detail into a previous blog) and transform them into View Models at the very top layer in the Presentation layer. In order to simplify the data transfer between one layer to another we make use of DTOs (Data Transfer Objects). These objects are a representation of a particular entity amongst these layers. A Person_DataModel is mapped to a Person_DTO from the Data to the Business Layer. The Person_DTO is then passed on to the Presentation layer where it is mapped to a Person_ViewModel. Thus every layer can use the nessecary information from the Person Entity without having to deal with the extra data which might not be needed at that particular stage.
So in this case we have the following mappings:
A particular View, 'Person_SummaryView' might need only a few details related to one person therefore the 'PersonSummaryViewModel' would get the data from the Person_DTO using the Automapper and rendered in the View as deemed nessecary. On the other hand, a 'Person_DetailsView' View might need more information and the mapping from the 'Person_DTO' to the 'Person_DetailsViewModel' would contain more information.
How to Use AutoMapper?
Automapper is very simple to use if you just want to 'Flatten' or 'Project' objects just as we discussed in the previous section. All you need to do is to define the mapping in your 'Bootstrapper' initialized in you 'Application_Start' method and then just define the map where you need it from the source object to the destination object as shown in the example below:
Use the Mapping and Get the Mapped object:
Customer customer = GetCustomerFromDB();
CustomerViewItem customerViewItem = Mapper.Map<Customer, CustomerViewItem>(customer);
Where can I get it from?
Please do not hesitate to contact us and leave any comments or queries below, we would be more than grateful to provide you with more complex examples on how to make the best use of the AutoMapper API.
Thanks for reading!