Editorials

Optimizing Data Transformations

AutoMap is a Dot Net library used to transform data model objects to and from data transfer objects. It uses a fluent syntax, helping you manage your data conversion efficiently. Personally, I don’t find it any more efficient that rolling your own transformation methods. However, the standardized way in which it is implemented, and the widespread use of this library make it a compelling tool, simply because many people know what to expect with this library.

Recently I have had an opportunity to do what turned out to be a little tuning on the mapping of complex objects, containing properties that are based on collections of other complex objects. I thought I’d share my experience.

I had a screen that was taking 30 seconds to return data. I started with the entity framework query being generated. I found that on the EF side I had better performance if I multiple round trip calls to the database. I returned all of the scalar properties in a single call, regardless of the table in which the data resided by using the EF Include operation.

I then made two separate calls gathering the data for the two properties of the original object that were based on collections (Parent-1 to Child-0 or many relationship). After retrieving each collection, I then assigned the results to the parent object. This then sped up the query considerably. However, the final results appearing on my screen was still taking 25 seconds. I was disappointed. After a little instrumentation, I discovered that the time was being consumed in the data transformation from the model objects to the data transfer objects. There wasn’t even that much data!

Here is what I discovered. The automap definitions did not handle the collections. There was a map for the parent object. There was a map for a line item object contained in the collections. So, to translate everything, the application first transformed the parent class. Then, in a foreach loop it translated and added each child object to the parent objects collection property, transforming a single row at a time.

A colleague suggested doing the whole thing in a single transform, using AutoMapper. I modified the original Parent object transform to alter all properties, including the collection properties. This meant I had to transform the collection objects in a foreach loop of the collection property definition.

Net result, the screen now returns so fast it is hard to track it. Lesson learned…don’t call AutoMapper transforms in a loop. Especially if there are a lot of records.

Cheers,

Ben