In MVC I simply make the class NinjectControllerFactory
that implements DefaultControllerFactory
interface then do some bindings in it. at last in Global
I run it:
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
But what about using Ninject in ASP.NET Web API? there are some information on the web but are out dated and for pre-released versions.
Is there a straightforward way for this problem?
NInject is a popular IOC container that can be used to inject dependencies in your WebAPI controllers easily. IDG. Dependency injection is a software design pattern that helps you to build pluggable implementations in your application using loosely coupled, testable components.
Set the dependency resolver on the DependencyResolver property of the global HttpConfiguration object. The following code registers the IProductRepository interface with Unity and then creates a UnityResolver . public static void Register(HttpConfiguration config) { var container = new UnityContainer(); container.
Ninject is a lightweight dependency injection framework for . NET applications. It helps you split your application into a collection of loosely-coupled, highly-cohesive pieces, and then glue them back together in a flexible manner.
The reason a lot of the articles are old is because the approach hasn't changed since June 2012 (RC released May 31st). You need to add the Ninject MVC3 Nuget package, then implement 'IDepenencyResolver' and then register your implementation.
The best two walk-thoughs are:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With