注射用Ninject AutoMapper依赖 [英] Injecting AutoMapper dependencies using Ninject

查看:176
本文介绍了注射用Ninject AutoMapper依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦注入AutoMapper到使用Ninject一个ASP.NET MVC 2应用程序。我用吉米博加德的岗位上<一个href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/05/11/automapper-and-ioc.aspx">AutoMapper和StructureMap类型配置为指导。

I am having trouble injecting AutoMapper into an ASP.NET MVC 2 application using Ninject. I used Jimmy Bogard's post on AutoMapper and StructureMap type Configuration as a guide.

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>();
    }
}

解决配置

Ninject抛出一个异常

  

错误激活IObjectMapper   没有匹配的绑定可用,并且类型不能自行绑定。   激活路径:
  3)注射依赖IObjectMapper进入配置类型构造函数的参数映射器

Error activating IObjectMapper No matching bindings are available, and the type is not self-bindable. Activation path:
3) Injection of dependency IObjectMapper into parameter mappers of constructor of type Configuration

更新

这是目前正在使用以下绑定:

This is now working using the following binding:

    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>();

我发表在GitHub上的模块。 AutoMapper.Ninject 。在我的博客的更多信息:<一href="http://binaryspeakeasy.com/2010/09/automapper-ninject/">http://binaryspeakeasy.com/2010/09/automapper-ninject/

I published the module on GitHub. AutoMapper.Ninject. More information on my blog: http://binaryspeakeasy.com/2010/09/automapper-ninject/

推荐答案

我得到了它的工作,但它不会觉得很干净创建配置类的一个实例。任何建议,以进一步清理。

I got it working but it doesn't feel very clean creating an instance of the Configuration class. Any suggestions to clean it up further.

        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>();

这篇关于注射用Ninject AutoMapper依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆