想要使用AutoMapper来处理一些'猴子'代码。它适用于蝙蝠;现在希望在一个地方设置所有映射。所以我有:
因为我不想孤儿这个问题;为了任何(不幸的)最终解决这个问题的人的利益;这对我有用:
Mapper.Initialize()
语法;我设置了MappingConfiguration类型的属性
config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<viewModel1, entity1>();
cfg.CreateMap<viewModel2, entity2>();
etc..
});
2)下一步是从Global.asax.cs调用Configure()
这允许我在unityConfig.cs中执行以下操作:
unityContainer.RegisterInstance<IMapper>(AutoMapperConfiguration.config.CreateMapper());
剩下的就是将一个IMapper实例注入我的Controller并使用它:
mapper.Map(src obj, dest obj);