Ninject is my favorite container for doing dependency injection. However, it always seemed a bit complicated to use with ASP.NET WebForms.
Things have improved with ASP.NET MVC 1 of course but now, with the release of ASP.NET MVC 3, things have become way easier to do.
Here is what my Global.asax.cs looks like when I want to register an IoC tool for ASP.NET MVC 3:
1 | protected void Application_Start() |
Well that look easy enough no? Well, to be fair… NinjectDependencyResolver doesn’t exist. It’s a custom class that I created to allow the easy mapping of the Kernel.
The only thing it needs is the Ninject StandardKernel. Once all the mappings are done, we give the kernel to the resolver and let MVC handle the rest.
Here is the code for NinjectDependencyResolver for all of you to use:
1 | public class NinjectDependencyResolver : IDependencyResolver |