2012-06-19 4 views
2

여기이 개 왼쪽에 내가 매핑 할 필요가 클래스입니다 : 오른쪽에AutoMapper : 대상베이스 소스 속성을지도

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

같은 객체가,하지만 상속을 사용하여 구성된 :

내가 쉽게 명시 적으로 매핑 할 수 있습니다

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

,하지만 난 몇 CIA 요원이 있습니다

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

는 내가 필요로하는 매핑입니다 모든 것은 Animal에서 파생 된 것이며 Animal 클래스가 크기 때문에 Animal을 한 번 매핑 한 다음 파생 클래스 매핑에 포함시키는 것이 더 좋습니다.

내가 보았습니다. <> 메소드를 포함하지만이 시나리오를 지원하지 않는다고 생각합니다.

// 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)); 

답변

3

당신은 매핑 기본 클래스와 BeforeMap을 추가 할 수있는 해결 방법으로 다음과 같습니다

내가 무엇을 찾고 (의사 코드)의 본질이다. 아마 그것은 최상의 솔루션은 아니지만 최소 맵핑 구성이 필요합니다.

humanMap.BeforeMap((src, dst) => 
{ 
    Mapper.Map(src.Animal, (AnimalDst)dst); 
});