views:

297

answers:

3

I know it's AutoMapper and not AutoMerge(r), but...

I've started using AutoMapper and have a need to Map A -> B, and to add some properties from C so that B become a kind of flat composite of A + C.

Is this possible in AutoMapper of should I just use AutoMapper to do the heavy lifting then manually map on the extra properties?

+1  A: 

From what I remember with AutoMapper you have to define your mappings as one input to one output (maybe this has changed since - haven't utilized it for many a month).

If this is the case, maybe your mapping should be of KeyValuePair<A,C> (or some sort of object composing both A & C) => B

This way you can have one defined input parameter mapping to your outputted object

saret
Composite object appears to be the way forward, like the idea of using an existing class too - I'll give it a try.
Jason Hyland
We pretty much always do a B -> BDto. We just kept running into issues with naming collisions to try to auto-merge things.
Jimmy Bogard
I would imaging the automatic convention based approach would be really hard to do (especially with collisions), but AutoMapper at least will allow you to define your own function/delegate to use for the mappings, so at least in this case one can just do it manually... and it doesn't take much code either - shot for the nice utility! :)
saret
A: 

Would this not work?

var mappedB = _mapper.Map<A,B>(aInstance);
_mapper.Map(instanceC,mappedB);
epitka
+2  A: 

You can do this with the ValueInjecter

 a.InjectFrom(b)
  .InjectFrom(c)
  .InjectFrom<SomeOtherMappingAlgorithmDefinedByYou>(dOrBOrWhateverObject);
Omu
This tool is way cool.
Merritt
@Merritt thnx, I did it :)
Omu
Using it now for mapping bunch of different dto-like objects to one EF entity.
Merritt
@Merritt cool, if you have any questions just tag it with valueinjecter, and I will answer
Omu

related questions