2012-09-14 3 views
3

다음 규칙에 따라 자동 매핑을 설정하고 싶습니다.대상 개체를 사용하지 않으면 조건부로 대상 덮어 쓰기

  • 은 "대신에"대상 구문을 사용하지 않는 경우 객체가 전달되면, 값
  • 에 특정 구성원을지도, 나는 해봤

대상 값을 사용 내가 생각할 수있는 모든 방법. 다음과 같은 것 :

Mapper.CreateMap<A, B>() 
    .ForMember(dest => dest.RowCreatedDateTime, opt => { 
     opt.Condition(dest => dest.DestinationValue == null); 
     opt.UseValue(DateTime.Now); 
    }); 

항상 값을 매핑합니다. 기본적으로 내가 원하는 것은 다음과 같습니다.

c = Mapper.Map<A, B>(a, b); // does not overwrite the existing b.RowCreatedDateTime 
c = Mapper.Map<B>(a);  // uses DateTime.Now for c.RowCreatedDateTime 

참고 : A에는 RowCreatedDateTime이 없습니다.

내 옵션에는 어떤 것들이 있습니까? Condition 메소드에 대한 문서가 없기 때문에 매우 실망스럽고 모든 Google 결과는 소스 값이 대상이 아닌 null 인 경우에 초점을 맞춘 것처럼 보입니다.

편집 : 패트릭에

덕분에, 그는 ... 바른 길에 저를 얻었다

나는 해결책을 알아 냈어. 누구든지이 일을하는 더 좋은 방법이 있다면 알려 주시기 바랍니다. 참고 dest.DestinationValue 대신 dest.Parent.DestinationValue을 참조해야했습니다. 어떤 이유로, dest.DestinationValue은 항상 null입니다. Condition 하나 (매핑이 수행되어야하는지 결정하는) 및 경우 Condition 반환 사실을 무엇을 정의하는 하나 :

.ForMember(d => d.RowCreatedDateTime, o => o.Condition(d => dest.Parent.DestinationValue != null)) 
.ForMember(d => d.RowCreatedDateTime, o => o.UseValue(DateTime.Now)) 

답변

4

나는 당신이 두 개의 매핑을 설정해야합니다 생각합니다. 다음과 같이 입력하십시오 :

.ForMember(d => d.RowCreatedDateTime, o => o.Condition(d => d.DestinationValue == null); 
.ForMember(d => d.RowCreatedDateTime, o => o.UseValue(DateTime.Now)); 
+0

불행히도, 작동하지 않습니다. 매번 전달 된 값을 겹쳐 씁니다. –

+0

결국, parent.DestinationValue를 참조해야하지만, 어쨌든 upvoted했습니다. –

+0

재미있는 ... upvote 주셔서 감사! – PatrickSteele