Merge two objects to produce third using AutoMapper

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?


你可以用ValueInjecter来做到这一点

 a.InjectFrom(b)
  .InjectFrom(c)
  .InjectFrom<SomeOtherMappingAlgorithmDefinedByYou>(dOrBOrWhateverObject);

这不行吗?

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

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

链接地址: http://www.djcxy.com/p/6374.html

上一篇: 它是否映射对象列表?

下一篇: 使用AutoMapper合并两个对象以产生第三个对象