Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I bind the same dependency to many dependents in Ninject?
    primarykey
    data
    text
    <p>Let's I have three interfaces: <code>IFoo</code>, <code>IBar</code>, <code>IBaz</code>. I also have the classes <code>Foo</code>, <code>Bar</code>, and <code>Baz</code> that are the respective implementations.</p> <p>In the implementations, each depends on the interface <code>IContainer</code>. So for the <code>Foo</code> (and similarly for <code>Bar</code> and <code>Baz</code>) the implementation might read:</p> <pre><code>class Foo : IFoo { private readonly IDependency Dependency; public Foo(IDependency dependency) { Dependency = dependency; } public void Execute() { Console.WriteLine("I'm using {0}", Dependency.Name); } } </code></pre> <p>Let's furthermore say I have a class <code>Container</code> which happens to contain instances of the <code>IFoo</code>, <code>IBar</code> and <code>IBaz</code>:</p> <pre><code>class Container : IContainer { private readonly IFoo _Foo; private readonly IBar _Bar; private readonly IBaz _Baz; public Container(IFoo foo, IBar bar, IBaz baz) { _Foo = foo; _Bar = bar; _Baz = baz; } } </code></pre> <p>In this scenario, I would like the implementation class <code>Container</code> to bind against <code>IContainer</code> with the constraint that the <code>IDependency</code> that gets injected into <code>IFoo</code>, <code>IBar</code>, and <code>IBaz</code> be the same for all three. In the manual way, I might implement it as:</p> <pre><code>IDependency dependency = new Dependency(); IFoo foo = new Foo(dependency); IBar bar = new Bar(dependency); IBaz baz = new Baz(dependency); IContainer container = new Container(foo, bar, baz); </code></pre> <p>How can I achieve this within Ninject?</p> <p><strong>Note: I am not asking how to do nested dependencies. My question is how I can guarantee that a given dependency is the same among a collection of objects within a materialized service.</strong></p> <p>To be extremely explicit, I understand that Ninject in it's standard form will generate code that is equivalent to the following:</p> <pre><code>IContainer container = new Container(new Foo(new Dependency()), new Bar(new Dependency()), new Baz(new Dependency())); </code></pre> <p>I would <em>not</em> like that behavior. I cannot have the <code>Dependency</code> created as a singleton either. Specifically, this means that if I have multiple requests to <code>GetService&lt;IContainer&gt;</code>, the Ninject calls should be semantically equivalent to the following manual injection:</p> <pre><code>var dep1 = new Dependency(); var container1 = new IContainer(new Foo(dep1), new Bar(dep1), new Baz(dep1)); var dep2 = new Dependency(); var container2 = new IContainer(new Foo(dep2), new Bar(dep2), new Baz(dep2)); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload