对于初学者,我正在使用这个模块:
public class AutoMapperModule : NinjectModule
{
public override void Load()
{
Bind<ITypeMapFactory>().To<TypeMapFactory>();
foreach (var mapper in MapperRegistry.AllMappers())
{
Bind<IObjectMapper>().ToConstant(mapper);
}
Bind<AutoMapper.ConfigurationStore>().ToSelf().InSingletonScope().WithConstructorArgument("mappers", ctx => ctx.Kernel.GetAll<IObjectMapper>());
Bind<IConfiguration>().ToMethod(ctx => ctx.Kernel.Get<AutoMapper.ConfigurationStore>());
Bind<IConfigurationProvider>().ToMethod(ctx => ctx.Kernel.Get<AutoMapper.ConfigurationStore>());
Bind<IMappingEngine>().To<MappingEngine>();
}
}
我的所有 map 都有一个 Bootstrap 类
public static void Configure(IKernel kernel)
{
Mapper.Initialize(map => map.ConstructServicesUsing(t => kernel.Get(t)));
}
我有访问数据库并需要注入(inject)存储库的解析器。
它按原样工作,但我不知道如何让它与单元测试和 IMappingEngine 一起工作。
public HomeController(IMappingEngine mappingEngine)
{
_mappingEngine = mappingEngine;
}
_mappingEngine.Map 抛出异常,因为不存在 map 。 Mapper.Map 有效。
我错过了什么?如何让我的 Bootstrap 使用单元测试,以便解析器中的存储库使用假/模拟存储库?
最佳答案
尝试更改映射的绑定(bind)。
Bind<IMappingEngine>().ToMethod(ctx => Mapper.Engine);
关于ninject - 带有 Ninject 混淆的 AutoMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10938740/