Posts

Showing posts with the label Ninject

How to: Implement Dependency Injection in MVC 4 with Ninject

After creating mvc 4, the first thing I realised was there were slightly framework changes. Most significantly is inside the 'Global.asax.cs', which moves all the configuration setting registers into separate class files in the folder 'App_Start'.  This actually prevents me from doing DI using 'Ninject.MVC3' in the way I used to implement in mvc 3. While Ninject package for mvc 4 is not out yet, I have to come up with an alternative solution. The following is the step by step guide. 1. Add 'Ninject.MVC3' package You will see 'NinjectWebCommon.cs' file is generated inside 'App_Start' folder, which was meant to be part of package adding process. 2. Modify 'Global.asax.cs' * Inherit 'MvcApplication' from 'NinjectHttpApplication' instead of 'HttpApplication' * Implement 'OnApplicationStarted' method inherited from 'NinjectHttpApplication', and move all registers in 'Application_Sta...