2

左側に、マップする必要がある 2 つのクラスがあります。

class HumanSrc {
    public int IQ;
    public AnimalSrc Animal;
}
class AnimalSrc {
    public int Weight;
}

右側は同じオブジェクトですが、継承を使用して構成されています。

class HumanDst : AnimalDst {
   public int IQ;
}
class AnimalDst {
    public int Weight;
}

したがって、必要なマッピングは次のとおりです。

humanSrc.IQ -> humanDst.IQ 
humanSrc.Animal.Weight -> humanDst.Weight;

このマッピングは明示的に簡単に行うことができますが、すべて Animal から派生したいくつかのクラスがあり、Animal クラスは大きいため、Animal を一度マッピングしてから、すべての派生クラス マッピングにそれを含めることをお勧めします。

.Include<> メソッドを見ましたが、このシナリオをサポートしているとは思いません。

これが私が探しているものの本質です(疑似コード):

// define animal mapping
var animalMap = Mapper.CreateMap<AnimalSrc, AnimalDst>().ForMember(dst=>dst.Weight, opt=>opt.MapFrom(src=>src.Weight);
// define human mapping
var humanMap = Mapper.CreateMap<HumanSrc, HumanDst>();
humanMap.ForMember(dst=>dst.IQ, opt=>opt.MapFrom(src=>src.IQ));

// this is what I want. Basically I want to say:
// "in addition to that, map this child property on the dst object as well"
humanMap.ForMember(dst=>dst, opt=>opt.MapFrom(src=>src.Entity));
4

1 に答える 1

3

回避策として、マッピング基本クラスを使用して BeforeMap を追加できます。おそらくこれは最善の解決策ではありませんが、少なくとも必要なマッピング構成は少なくなります:

humanMap.BeforeMap((src, dst) =>
{
   Mapper.Map(src.Animal, (AnimalDst)dst);
});
于 2012-06-19T21:08:40.940 に答える