Project description

oMapGen is a T4 template that automatically generates on convention basis C# mapping extension methods between two objects with a similar structure, identical property names and compatible types.

You may find it useful if you want to implement a weakly-coupled solution that requires a one-to-one mapping between entities that belong to different name spaces.
There are already tools that perform automatic runtime mapping between classes like AutoMapper and ValueInjecter. They are convenient to use but can reduce the performance of your program if lots of complex objects are mapped at the same time.

The tool I am offering here can be used in a similar fashion (although with not as many features for the time being) except that instead of performing on-the-fly mapping by calling it dynamically at runtime, the code itself is automatically generated at pre-build (from T4) and compiled with the layer that needs it. The risk of a performance issue is then lifted because all the costly reflection processing is done before the compilation.

The generated extension methods can thus be used very simply regarding their source and destination types:

object2 = object1.ToObject2();

object1 = new Object1().From(object2);

Concerning performance, I did a little benchmark comparison between the existing tools by mapping 10000 simple POCOs with boolean, integer and string fields, and I got the following results:

AutoMapper 280 milliseconds
ValueInjecter 41 milliseconds
oMapGen 1 millisecond

Current features

Static one-to-one mapping generation between the following types:
  • all base types;
  • enums (int values must be equal);
  • arrays;
  • collections with one generic;
  • collections with two generics;
  • key/value pairs;
  • nested classes (non-recursive: see known bugs);
  • nested interfaces (implementation classes must have identical names).
Nullable types are mapped to not-nullable ones with default values:
  • enums and numerics: 0;
  • booleans: false;
  • time types: DateTime.MinValue or TimeSpan.MinValue.

Planned features

I realize I am far behind AutoMapper and ValueInjecter in terms of raw features so I'm planning to add these to future releases of oMapGen:
  • validation;
  • convention-based flattening;
  • projection;
  • conditional mapping.

Known bugs

I'm still working on the following bugs (if you see any other ones and/or know how to correct them, please contribute):
  • transform command fails for recursive nested classes (cyclic aggregation);
  • generated code is not formatted automatically.

Last edited Apr 24, 2014 at 11:40 AM by xmamat, version 8