.net – 使用Ninject注入AutoMapper依赖项

前端之家收集整理的这篇文章主要介绍了.net – 使用Ninject注入AutoMapper依赖项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用Ninject将AutoMapper注入ASP.NET MVC 2应用程序时遇到麻烦.我用Jimmy Bogard的帖子在 AutoMapper and StructureMap type Configuration作为指导.
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抛出异常.

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

更新

现在正在使用以下绑定:

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/

您可以使用最新版本(目前为2.2.0)做一个班轮.
kernel.Rebind<IMappingEngine>().ToMethod(context => Mapper.Engine);

作为一个额外的,我同意fodonnel,添加一个门面来隐藏Automapper接口是一个好主意,但是我不会直接从Automapper采取签名,除非你需要所有的功能.

原文链接:https://www.f2er.com/javaschema/281418.html

猜你在找的设计模式相关文章