Software engineering blog of Clément Bouillier: Object to object mapping : a painful task

Saturday, September 6, 2008

Object to object mapping : a painful task

Remark : I have to refine this post, but I publish it since there is some news on the subject : AutoMapper has been published on CodePlex (see this post on Jimmy Bogard blog).


In August 2008, I have got the same needs as Jimmy, so I have started to search on the community, I did not find any existing tool but I found these three links :

I also have a look at "the other side" (i.e. Java), and it exists a framework named Dozer. It makes implicit mapping as you explain, but for more complicated mapping, you make the mapping in XML files like (N)Hibernate does.

I would like here to detail a little bit the why I found this type of tool useful.


In the first link I give, James Gregory talked about three requirements:
  1. Refactoring friendly. No strings for property names, changing names should give compiler errors.
  2. Must simplify code.
  3. Must improve maintainability.
So, on the first requirement, configuration approach fails more or less, because in fact, you will unit test your classes which make use of the mapper (and not make unit tests for string inspection, that have no value as said by James). More over, I have got a feedback from a meeting that was I should add a fluent interface, which makes configuration much more reliable and refactoring friendly. I am more and more open to this type of API.

On the second requirement, it is the one I prefer because I am bored to write mapping code, and implicit mapping simplify it. There is a difference between mapping code and configuration complexity because in mapping code, you have to create object instances, to recurse on collections, to handle inheritance mapping (and that code is very boring), whereas with configuration, I just declare mapping between types and fields and I have implicit mapping.

On the third requirement, I think configuration is simpler than code, even if it needs to understand mapping framework.

The central point of my first implementation was a huge use of Lambda expression and it simplifies a lot the code of the mapper.
The principal features I have implemented for now are:
  • explicit mapping through XML configuration (fluent interface or at least API in mind)
  • implicit mapping based on properties name
  • recursive mapping
  • inheritance mapping (in progress)
  • conversion during mapping (through Convert class)
  • types in scope : basic types, nullables, all enumerables (including generic ones), classes with public properties
I have several elements in an Excel sheets...I will share it.
I try to use a TDD approach so unit tests exists for these features.

I am very impatient to have a look at the Jimmy implementation (AutoMapper) and perhaps to participate to this tool improvement.

No comments: