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:
- Use Nuget to get Ninject.MVC3
- 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
- private static void RegisterServices(IKernel kernel)
- {
- kernel
- .Bind
() - .To
() - .InSingletonScope()
- .WithPropertyValue("Url", "http://localhost:8080")
- .OnActivation(store => store.Initialize());
- }
3. Use IDocumentStore in my controllers.
Code Snippet
- IDocumentStore store;
- public AdminController(IDocumentStore store)
- {
- this.store = store;
- }
Pretty slick.