Injection value for injecting addiction

I have something like this:

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

And I'm trying to get a link to Root using ninject. Therefore, I configure it this way

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

I would like to introduce the Dependency value of some val value, which is known only at the moment of receiving the Root link from ninject.

What I would like to do is something like this:

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

Is this possible using ninject 1.0?

+7
dependency-injection ninject
Jan 28 '10 at 10:30
source share
2 answers

Parameters.ConstructorArgument in the context only by default occupies one level of depth.

One way to pass parameters to several levels is to use a ContextParameter, but then something needs to be captured and said - and now we will use it as a ConstructorArgument in this case. One such design is Providers. See this dojo page for more information on suppliers.

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 are thinking about this, consider the points that I am making at this point, separating the problems associated with the configuration of the object binding in this answer .

+8
Jan 29 '10 at 13:44
source share

With Ninject 3, the IParameter ( ConstructorArgument is one of them) will now simply be "inherited" for the child request if they have ShouldInherit == true (see here ).

The solution for this question can now be simple:

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

where as true in the ConstructorArgument constructor, ShouldInherit set to true . Also see here

+1
Sep 23 '15 at 14:45
source share



All Articles