我无法使用Ninject将AutoMapper注入ASP.NET MVC 2应用程序.我使用Jimmy Bogard关于AutoMapper和StructureMap类型配置的帖子作为指南.
public class AutoMapperModule : NinjectModule
{
public override void Load()
{
Bind<ITypeMapFactory>().To<TypeMapFactory>();
Bind<Configuration>().ToSelf().InSingletonScope().WithConstructorArgument("mapper", MapperRegistry.AllMappers);
Bind<IConfiguration>().To<Configuration>();
Bind<IConfigurationProvider>().To<Configuration>();
Bind<IMappingEngine>().To<MappingEngine>();
}
}
Run Code Online (Sandbox Code Playgroud)
Ninject在解析时抛出异常Configuration.
激活IObjectMapper时出错没有匹配的绑定可用,并且该类型不可自绑定.激活路径:
3)将依赖项IObjectMapper注入到Configuration类型构造函数的参数映射器中
更新
现在使用以下绑定:
Bind<ITypeMapFactory>().To<TypeMapFactory>();
Bind<Configuration>().ToConstant(new Configuration(Kernel.Get<ITypeMapFactory>(), MapperRegistry.AllMappers())).InSingletonScope();
Bind<IConfiguration>().ToMethod(c => c.Kernel.Get<Configuration>());
Bind<IConfigurationProvider>().ToMethod(c => c.Kernel.Get<Configuration>());
Bind<IMappingEngine>().To<MappingEngine>();
Run Code Online (Sandbox Code Playgroud)
我在GitHub上发布了这个模块.AutoMapper.Ninject.有关我博客的更多信息:http://binaryspeakeasy.com/2010/09/automapper-ninject/