我已经创建了演示模型,我想将它(使用 AutoMapper)映射到 ViewModel 中。ViewModel 是复合的 / 因为我正在使用部分,并且我想在其他视图/部分上重用例如 KeyboardsViewModel。
如何将此演示模型映射(设置映射)到 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>();
似乎它有效,但我不确定这是否是最佳/正确的方式......