Skip to content

Changes in Ninject 3

chafey edited this page Feb 15, 2012 · 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:

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

This means the implicit bindings like self bindable types and closed generic types when there is only an open generic implementation should be included. 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?