Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone

 

Microsoft .NET Framework

Microsoft .NET Framework

In previous tutorial we discuss the basics of extension methods and we also mentioned there are many usage of this feature in large projects. In most real world projects we need to map data from internal data access objects or domain models to DTO (Data Transfer objects)  or to ViewModel objects. These are object hierarchies and sometimes we need one to one mapping but sometimes we need much flat structure to pass over the wire so that serialization overhead is much less. I know there are lot of object mappers out there with loads of features – very easy to use and reduce the coding time. We also used these mappers in many of our projects and they work very well during development phase where the data load is small and also when the object hierarchy is not that deep. But the after few months in production we noticed that lot of time is spent on these mappers, to be exact we found instances that 80% of the total time from database access to UI is spent on these object mappers to convert structures. It is obvious as these works with the help of .NET reflection. Moreover these mapper libraries are general libraries with lot of features that it’s logic is much complex and time consuming than if we do mapping by ourselves. I know many disagree with me, reality is I have seen the time difference very well and able to improve the performance drastically by doing the mapping ourselves.

Domain Models to View Models Mapping Example

In this example we have our domain models which represents a Hotel and Hotel has Address list, Contact list, Location coordinates and few properties. Consider this as business objects or data access objects used in business tier or below. Now we need to convert this to View Model which is a relatively flat structure and closer to the final output we want to have in UI. (Please note that this is just a hypothetical example to show you another usage of extension methods).

First we will look at domain model classes.

As you can see Hotel has Address list, Contact list and Location. Address has few properties and it also has Type to indicate what type address whether it is the address of physical location of the address or office/customer support address. Similarly Contact also can be of  different types fax, mobile, land line or email. Location has the coordinates of the hotel.

Our view model structure is as follows.

It is very simple, we need array of address lines (that can be printed to the end user), and contact info lines. We also represent the coordinates as a Google map url.

Extension Methods to convert Domain to View Models

I believe you can clearly understand what this code does if you know about extension methods or have gone through the previous post.

How to use this

In following test code I have created two hotels with their internal properties and then convert to View models using  a single line of code and then output to the console. Not only hotel but you can convert any other domain instance to corresponding view model or simply to output type anywhere in the code. This is much faster and cleaner way of doing the mapping.

This is the final output.

Output of view models

Output of view models

 


 

Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone