注射用Ninject AutoMapper依赖注射用、Ninject、AutoMapper

由网友(忆蝶)分享简介:我有麻烦注入AutoMapper到使用Ninject一个ASP.NET MVC 2应用程序。我用吉米博加德的岗位上AutoMapper和StructureMap类型配置为指导。I am having trouble injecting AutoMapper into an ASP.NET MVC 2 applicat...

我有麻烦注入AutoMapper到使用Ninject一个ASP.NET MVC 2应用程序。我用吉米博加德的岗位上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 。在我的博客的更多信息: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>();
阅读全文

相关推荐

最新文章