将值注入注入的依赖项 [英] Inject value into injected dependency

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

问题描述

我有这样的事情:

class Root
{
    public Root(IDependency dep)
    {}
}
class Dependency:IDependency
{
    public Dependency(int val)
    {}
}

我正在尝试使用 ninject 获取对 Root 的引用.所以我这样配置

And I'm trying to obtain a reference to Root using ninject. So i configure it like this

var module = new InlineModule(mod => mod.Bind<IDependency>().To<Dependency>());

var kernel = new StandardKernel(module);

我想向 Dependency 注入一些仅在从 ninject 获取 Root 引用时才知道的val"值.

I'd like to inject into Dependency some 'val' value that is known only at the moment of obtaining the Root reference from ninject.

我想做的是这样的:

Kernel.Instance.Get<Root>(With.Parameters.ConstructorArgument("val", 12));

使用 ninject 1.0 可以实现类似的功能吗?

Is something like this possible using ninject 1.0?

推荐答案

上下文中的 Parameters.ConstructorArgument 默认只进入一级.

The Parameters.ConstructorArgument in the context only goes one leve deep by default.

将参数向下传递多个级别的一种方法是使用 ContextParameter,但是需要一些东西来获取它并说 - 现在我们将使用它作为在这种情况下,ConstructorArgument.一种这样的构造是提供者.请参阅此道场页面了解提供者的详细信息

One way of passing parameters down multiple levels is by using a ContextParameter, but something then needs to grab that and say - and now we're going to use that as a ConstructorArgument in this case. One such construct is Providers. See this dojo page for details of Providers

所以你可以这样做:

    class DependencyProvider : SimpleProvider<Dependency>
    {
        protected override Dependency CreateInstance( IContext context )
        {
            return new Dependency( (int)context.ParentContext.Parameters.GetOne<ContextVariableParameter>( "masterVal" ).Value );
        }
    }

    public static void Main()
    {
        var module = new InlineModule(
            mod => mod.Bind<IDependency>().ToProvider( new DependencyProvider() )
        );

        var kernel = new StandardKernel( new[  ] {module} );

        Root root = kernel.Get<Root>( With.Parameters.ContextVariable( "masterVal", 12 ) ); 
    }

或者您可以按如下方式进行管理:

Or you can manage it as follows:

    class RootProvider : SimpleProvider<Root>
    {
        protected override Root CreateInstance( IContext context )
        {
            return new Root( context.Kernel.Get<Dependency>( With.Parameters.ConstructorArgument("val", ( int )context.Parameters.GetOne<ContextVariableParameter>("masterVal").Value )));
        }
    }

    public static void Main()
    {
        var module = new InlineModule(
            mod => mod.Bind<IDependency>().To<Dependency>(), // Optional if ImplictSelfBinding is on
            mod => mod.Bind<Root>().ToProvider( new RootProvider() )
        );

        var kernel = new StandardKernel( new[] {module} );

        Root root = kernel.Get<Root>( With.Parameters.ContextVariable( "masterVal", 12 ) ); 
    }

当您考虑这个时,请考虑我提出的观点在这一点上,重新分离这个响应中的配置与对象绑定的关注点.

While you're thinking about this, consider the points I make in this point re separating the concerns if configuration from object binding in this response.

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

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