我使用AutoMapper
将一组Models
映射到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
类映射到我的String
类时,应始终将Models
映射到ViewModels
?
反之亦然,那么当它从SomeProperty
映射回Int32
时,是否可以告诉automapper始终将属性ViewModels
映射回Models
? / p>