2010-01-25 4 views
2

프리젠 테이션 모델을 만들었으며 AutoMapper와 함께 ViewModel에 매핑하려고합니다. ViewModel은 partials를 사용하고 있기 때문에 다른보기/부분에 대해서도 KeyboardsViewModel을 재사용하기 때문에 composite /입니다.지도 ViewModel/partials with AutoMapper

이 프리젠 테이션 모델을 ViewModel에 어떻게 매핑 (설정 매핑) 할 수 있습니까? 이것이 올바른 접근 방법입니까?

감사합니다.

public class MainPresentationModel : BasePresentationModel 
{ 
    // Should map into the MainViewModel.Keyboards.Keyboards 
    public int DefaultKeyboard { get; set; } 
    // Should map into the MainViewModel.Keyboards.DefaultKeyboard 
    public IList<Keyboard> Keyboards { get; set; } 
    // Should map into the MainViewModel.Something 
    public string Something { get; set; } 
} 

public class MainViewModel : BaseViewModel 
{ 
    public KeyboardsViewModel Keyboards { get; set; } 
    public string Something { get; set; } 
} 

public class KeyboardsViewModel 
{ 
    public int DefaultKeyboard { get; set; } 
    public IList<Keyboard> Keyboards { get; set; } 
} 

편집 : 는 시도 후 나는이 하나 개의 옵션이라고 생각 :

 Mapper.CreateMap<MainPresentationModel, MainViewModel>() 
      .ForMember(d => d.Keyboards, opt => opt.MapFrom(src => src)); 
     Mapper.CreateMap<MainPresentationModel, KeyboardsViewModel>(); 

작동 것 같다, 그러나 이것은 최적의/올바른 방법 인 경우 나는 ...

를 확실하지 않다

답변

0

이 방법은 틀림없이 작동합니다. 이러한 복합 UI에 인터페이스를 사용할 수도 있습니다. 예를 들어, 부분은 IKeyboardsViewModel을 받아 들일 수 있습니다. 그러면 복잡한 상속 계층에 대해 걱정할 필요가 없습니다. 그런 다음 각 부분에 주 모델 조각을 제공 할 수 있습니다.