Note that there are some explanatory texts on larger screens.

plurals
  1. PONinject Cascading Inection with IList
    primarykey
    data
    text
    <p>I am trying to use Ninject to implement cascading injection into a class that contains an IList field. It seems that, unless I specifically specify each binding to use in the kernel.Get method, the IList property is always injected with a list of a single default object. </p> <p>The following VSTest code illustrates the problem. The first test fails because the IList field contains one MyType object with Name=null. The second test passes, but I had to specifically tell Ninject what constructor arguments to use. I am using the latest build from the ninject.web.mvc project for MVC 3.</p> <p>Does Ninject specifically treat IList different, or is there a better way to handle this? Note that this seems to only be a problem when using an IList. Createing a custom collection object that wraps IList works as expected in the first test. </p> <pre><code>[TestClass()] public class NinjectTest { [TestMethod()] public void ListTest_Fails_NameNullAndCountIncorrect() { var kernel = new Ninject.StandardKernel(new MyNinjectModule()); var target = kernel.Get&lt;MyModel&gt;(); var actual = target.GetList(); // Fails. Returned value is set to a list of a single object equal to default(MyType) Assert.AreEqual(2, actual.Count()); // Fails because MyType object is initialized with a null "Name" property Assert.AreEqual("Fred", actual.First().Name); } [TestMethod()] public void ListTest_Passes_SeemsLikeUnnecessaryConfiguration() { var kernel = new Ninject.StandardKernel(new MyNinjectModule()); var target = kernel.Get&lt;MyModel&gt;(new ConstructorArgument("myGenericObject", kernel.Get&lt;IGenericObject&lt;MyType&gt;&gt;(new ConstructorArgument("myList", kernel.Get&lt;IList&lt;MyType&gt;&gt;())))); var actual = target.GetList(); Assert.AreEqual(2, actual.Count()); Assert.AreEqual("Fred", actual.First().Name); } } public class MyNinjectModule : NinjectModule { public override void Load() { Bind&lt;IList&lt;MyType&gt;&gt;().ToConstant(new List&lt;MyType&gt; { new MyType { Name = "Fred" }, new MyType { Name = "Bob" } }); Bind&lt;IGenericObject&lt;MyType&gt;&gt;().To&lt;StubObject&lt;MyType&gt;&gt;(); } } public class MyModel { private IGenericObject&lt;MyType&gt; myGenericObject; public MyModel(IGenericObject&lt;MyType&gt; myGenericObject) { this.myGenericObject = myGenericObject; } public IEnumerable&lt;MyType&gt; GetList() { return myGenericObject.GetList(); } } public interface IGenericObject&lt;T&gt; { IList&lt;T&gt; GetList(); } public class StubObject&lt;T&gt; : IGenericObject&lt;T&gt; { private IList&lt;T&gt; _myList; public StubObject(IList&lt;T&gt; myList) { _myList = myList; } public IList&lt;T&gt; GetList() { return _myList; } } public class MyType { public String Name { get; set; } } </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.
    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