我AutoMapper
用来映射一组Models
to ViewModels
。一切正常,但我必须为我创建的每个模型/视图模型一遍又一遍地做一件事。我必须将某个属性从(例如)映射String
到Int
.
所以我的映射现在看起来像这样:
Mapper.CreateMap<ModelOne, ModelOneViewModel>()
.ForMember (d => d.SomeProperty, opt => opt.ResolveUsing(t => Convert.ToString(t.SomeProperty)));
Mapper.CreateMap<ModelOneViewModel, ModelOne>()
.ForMember (d => d.SomeProperty, opt => opt.ResolveUsing(t => Convert.ToInt32(t.SomeProperty)));
Mapper.CreateMap<ModelTwo, ModelTwoViewModel>()
.ForMember (d => d.SomeProperty, opt => opt.ResolveUsing(t => Convert.ToString(t.SomeProperty)));
// ... etc
有没有办法告诉 automapper当它从我的班级映射到我的班级时,它应该始终映射SomeProperty
到 a ?String
Models
ViewModels
反之亦然,那么是否可以告诉 automapper 始终将属性映射SomeProperty
回Int32
当它从ViewModels
back 映射到时Models
?