AutoMapper/AutoMapper — Issue #150: AutoMapper nested collection issue
Author: rhk98
Created: 2011-11-14 18:35:38+00:00
State: open

I am trying to map the following Domain class(es) into their corresponding DTOs

class ClassA {
...
GetHashCode();
}

class ClassB {
...
public IEnumerable ClassASet { get; set;}
}

class ClassC {
...
public ClassB classb { get; set;}
GetHashCode();
}

class ClassD {
...
IEnumerable ClassCSet { get; set;}
GetHashCode();
}
Within the domain the IEnumerables are implemented using the HashSet concrete type. Within the domain I have no issues when I create various objects. The GetHashCode method (together with the Equals override) ensures that the ClassA and ClassC collections do not contain any duplicates within a ClassB & ClassD object respectively.

However when I try to construct a ClassD DTO using AutoMapper I am noticing some strange behaviour.

Assume I have created a ClassD domain object, "classd" with 2 unique classC objects in its collection, classc1, classc2. classc1 has a reference to classb1 & classc2 has a reference to another ClassB object, classb2. In turn classb1 has 2 unique ClassA objects ints collection. classb2 also has 2 unique ClassA objects in its collection. As expected this works fine for the doamin objects.

However when I use AutoMapper to create the corresponding DTOs, I notice that Automapper seems to check that all the ClassA objects in the ClassD object(via ClassC -> ClassB) are unique. This results in Automapper not adding a ClassADTO object to a ClassBDTO instance, if another ClassADTO object with the same hashcode has already been added to another ClassBDTO instance.

My mapping are pretty strainghforward: e.g:

Mapper.CreateMap();
etc.

Any help in understanding what might be going on would be greatly appreciated. If you need any clarifications then please do let me know.

Note: I believe I have been able to reproduce this issue on a sample test Project (The solution consists of 2 projects, one with the sample domain & DTO classes, the other the corresponding nUnit project). If you believe this can help then please let me know how I can upload it to you.

Thanks.