Inject value into injected dependency

前端 未结 2 2032
臣服心动
臣服心动 2020-12-11 04:34

I\'m having something like this:

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


        
相关标签:
2条回答
  • 2020-12-11 04:35

    With Ninject 3 IParameters (ConstructorArgument is one of them) will now simply be "inherited" to child request when they have ShouldInherit == true (see here).

    The solution for this question can now be as simple as:

    IResolutionRoot.Get<Root>(new ConstructorArgument("val", 12, true));
    

    where as the true in the ConstructorArguments constructor sets ShouldInherit to true. Also see here

    0 讨论(0)
  • 2020-12-11 04:40

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

    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

    So you can do:

        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.

    0 讨论(0)
提交回复
热议问题