- enable package restore on solution
- Nuget Ninject.MVC3 (pre-release available?)
* adds Ninject, Ninject.Web.Common, Ninject.Web.Mvc
* App_Start/NinjectWebCommon.cs -> has WebActivator.PreApplicationStartMethod attribute -> runs at startup - add interface and class for testing (IBikeManager and BikeManager)
- add controller constructor to HomeController that takes in the interface
- run website => Error
"Error activating IBikeManager
No matching bindings are available, and the type is not self-bindable.
Activation path:
2) Injection of dependency IBikeManager into parameter bikeManager of constructor of type HomeController
1) Request for HomeController" - in NinjectWebCommon, register the services in the RegisterServices Method
- kill the IISExpress instance (this only runs on app startup, so you’ll have to do this each time you register a new service), restart app (ctrl + F5)
- HomeController now gets loaded with the IBikeManager in the constructor (set breakpoint and start with F5 to prove it)
That’s all there is to using Ninject as you IoC container and constructor injection (Dependency Injection aka DI) working with MVC (3, 4, or 5).
CodeProject