NinjectMVC3: Dependency Injection in 30 seconds or less

I'm frequently baffled by the hoops one must go through in order to setup things like dependency injection in a web app. While browsing through Ayende's Raccoon Blog source code, I noticed he avoided using IoC and instead created a helper class to manage store/session lifetimes. Seemed like more work than I wanted to do.
Instead of spending time avoid the IoC setup pain, I took Ninject.MVC3 for a test drive. It went something like:
  1. Use Nuget to get Ninject.MVC3
  2. Edit RegisterServices -- the Nuget package conveniently adds some code in the App_Start folder so you don't have to worry about the boiler plate.  In this case, I wanted to pass a controller an instance of IDocumentStore for RavenDB:
Code Snippet
  1. private static void RegisterServices(IKernel kernel)
  2.         {
  3.             kernel
  4.                 .Bind()
  5.                 .To()
  6.                 .InSingletonScope()
  7.                 .WithPropertyValue("Url", "http://localhost:8080")
  8.                 .OnActivation(store => store.Initialize());
  9.         }
3. Use IDocumentStore in my controllers.
Code Snippet
  1. IDocumentStore store;
  3. public AdminController(IDocumentStore store)
  4. {
  5. = store;
  6. }


Pretty slick.

Discuss this post

Why did you guys choose ninject over the extremely more performant (and likely more feature rich) StructureMap or AutoFac containers?

Also great registration process for comments. I would take the 1 less click for step from email to step to user name, and just put both fields on step1.

Update, I take back my remarks about the registration process. I feel like i was lied to. Went from 1 field, to 2 fields, to captacha to email verification to another form, to another capatcha. This was an extremely deceiving process, if I was presented this much up front I probably wouldn't have even started.

    Reply#1 - Thu Mar 1, 2012 1:17 PM EST
    You're in Easy Mode. If you prefer, you can use XHTML Mode instead.
    As a new user, you may notice a few temporary content restrictions. Click here for more info.