Ninject 1.0 to 2.0 .Only

66 Views Asked by At

Hi I am following below link, http://codeclimber.net.nz/archive/2009/02/10/how-to-use-ninject-to-inject-dependencies-into-asp.net-mvc.aspx

I get error on line:

kernel.Bind<IHttpController>().To<MyTESTController>()
    .Only(When.Context.Variable("controllerName")
    .EqualTo("CorporateRequirementsController"))
    .InjectPropertiesWhere(p => p.Name == "ActionInvoker");

I believe .Only is the syntax from Ninject 1.0, what is the equivalent in 2.0?

The error I am getting is like this:

'Ninject.Syntax.IBindingWhenInNamedWithOrOnSyntax<.../..../.Controllers.CorporateRequirementsController>' does not contain a definition for 'Only' and no extension method 'Only' accepting a first argument of type 'Ninject.Syntax.IBindingWhenInNamedWithOrOnSyntax<.../..../.Controllers.CorporateRequirementsController>' could be found (are you missing a using directive or an assembly reference?

1

There are 1 best solutions below

0
On BEST ANSWER

Yes, you are right. Based from the old svn sources it was in 1.0 - http://ninject.googlecode.com/svn/trunk/src/Core/Binding/Syntax/IBindingConditionSyntax.cs , but at the current version it doesn't exist.

In the their actual documentation there are a couple of samples on how you can do conditional binding: attributes\named bindings\when methods.

I suppose the closest to Only is

IBindingInNamedWithOrOnSyntax<T> When(Func<IRequest, bool> condition);

declared here . So you can pass your custom function that will be called during object resolution and where you will check for controller type. There are also a couple of overloads of When method for different cases, so you may want to check them too.