Skip to content

Changes in Ninject 3

Scott Xu edited this page Apr 7, 2014 · 10 revisions

Link to thread announcing Ninject 3 RC Availability

Links to Remo Gloor’s Blogs

Change in Default Constructor Selection

Ninject 2.2 has an issue where the constructor it selects can change. Here is an example:

class Foo 
{
    Foo() {}
    Foo(Bar bar) {}
}
kernel.Get<Foo>(); // The Foo() constructor is called by Ninject 2.2 the first time Foo is resolved
kernel.Get<Bar>();
kernel.Get<Foo>(); // The Foo(Bar bar) constructor is called since Ninject 2.2 now knows about Bar.

To resolve this issue, Ninject 3 will now choose the constructor with the most arguments it knows how to create but implicit self bindings are considered as unknown! Add an explicit self binding in case it matters for the selection.

class Foo 
{
    Foo() {}
    Foo(Bar bar) {}
}
kernel.Get<Foo>(); // The Foo() constructor is called by Ninject 3.0 
kernel.Get<Bar>();
kernel.Get<Foo>(); // The Foo() constructor is called by Ninject 3.0

Bottom line – if you are upgrading to Ninject 3.0 from Ninject 2.2, check your bound types to make sure the correct constructor is used!

Go back to the Home page or the Table of Contents

Continue reading: Why Use Ninject?