【问题标题】:Injecting AutoMapper dependencies using Ninject使用 Ninject 注入 AutoMapper 依赖项
【发布时间】:2011-04-06 14:49:52
【问题描述】:

我在使用 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 在解析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>();

我在 GitHub 上发布了该模块。 AutoMapper.Ninject。更多信息见我的博客:http://binaryspeakeasy.com/2010/09/automapper-ninject/

【问题讨论】:

标签: .net asp.net-mvc-2 ninject automapper bootstrapping


【解决方案1】:

您可以使用最新版本(当前为 2.2.0)来做这件事。

kernel.Rebind<IMappingEngine>().ToMethod(context => Mapper.Engine);

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

【讨论】:

    【解决方案2】:

    引入映射外观可能也是一个好主意。而不是通过您的代码传递 IMappingEngine 创建一个 IObjectMapper 接口。我使用的接口包含直接取自 automappers 代码的方法签名。

    public interface IObjectMapper
    { 
      TDestination Map(TSource source);
      TDestination Map(TSource source, TDestination destination);
      object Map(object source, Type sourceType, Type destinationType);
      object Map(object source, object destination, Type sourceType, Type destinationType);
    }
    

    您的配置仍将依赖于自动映射器。

    我在上面写的一篇博文在这里:http://fodonnel.wordpress.com/2010/09/20/an-object-mapper-facade/

    【讨论】:

    • 博客链接失效了。
    【解决方案3】:

    我让它工作了,但是创建配置类的实例感觉不是很干净。任何进一步清理它的建议。

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

    【讨论】:

    • 最好将其编辑到您的问题中。一般来说,我会说你过度使用Bind&lt;IX&gt;().ToMethod(c =&gt; c.Kernel.Get&lt;X&gt;()。只需使用Bind&lt;IX&gt;().To&lt;X&gt;()
    • 同上 Bind&lt;Configuration&gt;().ToConstant(new Configuration(Kernel.Get&lt;ITypeMapFactory&gt;(), MapperRegistry.AllMappers())).InSingletonScope(); 应该映射到 .To&lt;&gt;.WithConstructorArgument....
    猜你喜欢
    • 2015-10-09
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多