I have a list of objects that are loaded from a database, let's call them 'MyObjects' then I have a list of extensions objects 'ExtensionsObjects' that are loaded from a separate database. I use automapper to map some properties of these extension objects to 'MyObjects'. (The extention object contains a key field to MyObject)
This works, the fields are mapped correctly from ExtentionObject to MyObject but the automapper returns a list that only returns those 'MyObjects' to which an 'ExtensionObject was mapped. (and a MyObject without a Extention object is a perfectly valid situation).
The code I am using for the mapping:
//first get the lists of MyObjects and ExtentionObjects
List<MyObject> myObjects = GetMyObjects();
List<ExtentionObject> extentionObjects = GetExtentionObjects();
//execute the mapping (_mapper is my automapper)
myObjects = _mapper.Map(extentionObjects, myObjects);
//myObject now contains less objects than before the call to the mapper
The code for the automapper configuration (cfg being the mapper configuration used to create the mapper):
cfg.CreateMap<ExtentionObject, MyObject>()
.EqualityComparison((eo, my)=> CheckForEquality(eo, my))
.ForMember(....)
.ForMember(....)
.ReverseMap().ConvertUsing((mo, eo)=>
{
var ext = new ExtentionObect();
...
return ext;
});
The check for equality function simply checks if the ID's of ExtentionObject and MyObject match.
I want the resulting list to contain all the items that where in the orginial 'myObjects' list. The information in the ExtentionObject instances should be added to the corresponding MyObject instances, but as the Extention is optional all 'MyObjects' must remain in the resulting list.
Say my database contains MyObjects with Keys 1, 2,3 and ExtentionObjects with Key 1 and 3.
//before this cal myObjects contains 3 items, ExentionObjects contains 2
myObjects = _mapper.Map(extentionObjects, myObjects);
//after this cal myObjects contains only 2 items,
//with the properties from Extentionobject 1 and 3 correctly mapped to MyObject 1 and 3,
//ERROR => MyObject 2 has "disappeared" from the 'destination' list
The question is 'How do I keep Object 2 in my list'?