Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should use constructor injection instead of property injection if possible. This is a better technique, which is recommended by Mark Seeman, because makes dependencies required for object construction explicit and object signature via constructor is more expressive. Code should look like this:</p> <pre><code> abstract class A { public IDependency Dependency { get; private set; } public A (IDependency dependency) { Dependency = dependency; } } class B : A { public B (IDependency dependency) : base(dependency) { } } class C : A { public C (IDependency dependency) : base(dependency) { } } interface IDependency { } class EmptyDependency : IDependency { } class ConcreteDependency : IDependency { } </code></pre> <p>Configuration will be the same as in you example. The following test passes</p> <pre><code> [Test] public void TestSpecificBindingToObjectB() { var kernel = new StandardKernel(new TestModule()); var b = kernel.Get&lt;B&gt;(); var c = kernel.Get&lt;C&gt;(); Assert.AreNotEqual(b.Dependency.GetType(), c.Dependency.GetType()); Assert.AreEqual(typeof(ConcreteDependency), b.Dependency.GetType()); } </code></pre> <p>If you have an optional dependency with default implementation and you are ok with decorating your classes with <code>Inject</code> attribute, you can can pull parent information from request, like this:</p> <pre><code>class TestModule : NinjectModule { public override void Load() { Bind&lt;IDependency&gt;().To&lt;EmptyDependency&gt;(); Bind&lt;IDependency&gt;().To&lt;ConcreteDependency&gt;().When(req =&gt;req.ParentContext.Request.Service == typeof(B)); } } </code></pre> <p>Then the same test given above passes for your class hierarchy with property injection.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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