← Previous Chapter 

I have written a program that compares performance of H-Mapper with 2 other tools that I know : Automapper (probably the most known and used) and ExpressMapper. For Automapper, I used the latest version released at this time (5.1.1). Same thing for ExpressMapper (1.8.3). Please note that performance of Automapper has been greatly improved compared to version 4 and prior.

Source files are available on github.

The comparison is done on various situations. I also included performance of what I called a naive manual mapping implementation (when we don’t use any mapper).

 

 

performance

 

As you can see, H-Mapper is doing very well in all situations. The performance is really closed to the naive manual implementations (sometimes better). The bigger gaps appear when polymorphism or generic types are involved.

 

The details

Very simple class

A very basic class with just 2 fields:

public class VerySimpleClass
{
 public string MyString;
 public int MyInt;
}

Simple class

A more complex class with more fields, an array of integers, a property of a mapped type, and a calculated field (Date_Plus_2 is the Date coming the source object, in which we add 2 days).

public class SimpleClass
{
 public int Key;
 public int Integer { get; set; }

 public string String { get; set; }
 public DateTime Date { get; set; }
 public DateTime? NullableDate { get; set; }
 public bool Bool { get; set; }
 public string StringToBeIgnored { get; set; }

 public VerySimpleClass VerySimpleClass { get; set; }
 public int[] IntArray { get; set; }
 public DateTime Date_Plus_2 { get; set; }
}

 

Simple set

This class contains an array of type « VerySimpleClass ». The test is based on 10000 iterations of an instance containing 100 items.

public class SimpleSet
{
  public VerySimpleClass[] VerySimpleClasses { get; set; }
}

This time, the manuel map does the worst performance. How is it possible? Well, as I said I did a naive implementation using Linq:

public SimpleSet(Business.SimpleSet set)
 {
    if (set.VerySimpleClasses != null)
    {
      VerySimpleClasses = set.VerySimpleClasses.Select(a => new VerySimpleClass()
      {
         MyInt = a.MyInt,
         MyString = a.MyString
      }).ToArray();
   }
}

 

Multiple set

This class contains various type of collections. The test is based on 1000 iterations where each instance contains 100 items of each collection type.

public class MultipleSets
{
   public int Id { get; set; }
   public List<SimpleClass> SimpleClasses { get; set; }
   public IEnumerable<string> StringSet { get; set; }
   public HashSet<int> IntegerSet { get; set; }
   public Collection<double> DoubleSet { get; set; }
   public ICollection<DateTime> DateSet { get; set; }
}

 

Dictionary

I wanted a special test for dictionary, because it’s a collection type with 2 types of generic arguments, so it’s dealt a bit differently.

public class DictionarySet
{
   public Guid Id { get; set; }
   public IDictionary<string, int> DictSimple { get; set; }
   public IDictionary<SimpleClass, VerySimpleClass> DictObjects { get; set; }
}

Expressmapper is not listed here because apparently, dictionary cannot be mapped (yet).

 

Generic class of int

The class is a generic class. It contains a property whose type is the type of the generic. For this scenario, the generic type is a simple int.

public class SimpleGeneric<T>
{
   public int Id { get; set; }
   public T GenericProperty { get; set; }
}

 

Mapped object generic class

Same as above, but this time, the generic argument is a mapped object.

public class MappedObjectGeneric<T>
{
   public int Id { get; set; }
   public string AString { get; set; }
   public T GenericProperty { get; set; }
}

 

Multiple generic class

This time, the class has two generic arguments.

public class MultipleGenerics<A,B>
{
   public int Id { get; set; }
   public A A_Property { get; set; }
   public B B_Property { get; set; }
}

 

Polymorphic class

We have a distinction between business and presentation models. The business class has a base class, a sub-class, and a subclass of the subclass. The presentation layer has a base class and just a subclass. So, for any instance of the sub subclass, an instance of a subclass is created in the presentation layer.

public class PolymorphicBaseClass
{
   public int Id;
}
public class PolymorphicSubClass : PolymorphicBaseClass
{
   public string Name;
}
public class PolymorphicSubSubClass : PolymorphicSubClass
{
   public Guid Guid;
}

 

Set of polymorphic class

The set consist on having a set of a base classes, but each item should be of the most concrete type once mapped.

public class SetOfPolymorphic
{
   public int Id;
   public PolymorphicBaseClass[] PolymorphicBaseClasses;
}

In this scenario, ExpressMapper fails to deliver the right concrete class, so the performance is not listed.

Generic of polymorphic class

This time, the mapped class is a generic class where the argument is of the base polymorphic class. We simply reuse the « MappedObjectGeneric<T> » class where T is « PolymorphicBaseClass ».

 

Set of generic polymorphic classes

The mapped class contains a set of a generic class where the argument is a polymorphic class.

public class SetOfGenericPolymorph
{
   public int Id;
   public IList<MappedObjectGeneric<PolymorphicBaseClass>> Set;
}

In this scenario, ExpressMapper fails to deliver the right concrete class, so the performance is not listed.

← Previous Chapter