使用Ninject注入AutoMapper依赖项

Cor*_*oto 10 .net bootstrapping ninject automapper asp.net-mvc-2

我无法使用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/

Gra*_*ing 11

您可以使用最新版本(目前为2.2.0)进行单线程.

kernel.Rebind<IMappingEngine>().ToMethod(context => Mapper.Engine);
Run Code Online (Sandbox Code Playgroud)

另外,我同意fodonnel,添加一个外观来隐藏Automapper界面是一个好主意,但是我不会直接从Automapper中获取签名,除非你需要所有这些功能.


Cor*_*oto 1

我让它工作了,但创建 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)