使用 Ninject 注入 AutoMapper 依赖项

发布于 2024-09-16 15:03:40 字数 1781 浏览 3 评论 0原文

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

Ninject 在解析 Configuration 时抛出异常。

激活 IObjectMapper 时出错 没有可用的匹配绑定,并且该类型不可自绑定。 激活路径:
3)将依赖项IObjectMapper注入到Configuration类型的构造函数的参数映射器中

Update

类型的构造函数的参数映射器中这现在使用以下绑定进行工作:

    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 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 throws an exception when resolving Configuration.

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

Update

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凡尘雨 2024-09-23 15:03:40

您可以使用最新版本(当前为 2.2.0)执行此操作。

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

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

You can do this is a one liner using the latest version (currently 2.2.0).

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

As an extra, I do agree with fodonnel, adding a facade to hide the Automapper interface is a good idea, however I wouldn't take the signatures directly from Automapper, unless you need all that functionality.

听风吹 2024-09-23 15:03:40

引入映射外观也可能是个好主意。不要在代码中传递 IMappingEngine,而是创建 IObjectMapper 接口。我使用的接口包含直接从自动映射器代码中获取的方法签名。

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/

It might also be a good idea to introduce a mapping facade. Instead of passing IMappingEngine through out your code create an IObjectMapper interface. The interface I use contains method signatures taken directly out of automappers code.

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

Your configuration is still going to be automapper dependent.

A blog post I wrote on it is here: http://fodonnel.wordpress.com/2010/09/20/an-object-mapper-facade/

倥絔 2024-09-23 15:03:40

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

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>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文