• MSN
  • Hotmail
  • More
    • Autos
    • My MSN
    • Video
    • Careers & Jobs
    • Personals
    • Weather
    • Delish
    • Quotes
    • White Pages
    • Games
    • Real Estate
    • Wonderwall
    • Horoscopes
    • Shopping
    • Yellow Pages
    • Local Edition
    • Traffic
    • Feedback
    • Maps & Directions
    • Travel
    • Full MSN Index
  • Bing
  • NBCNews.com
  • TODAY
  • Nightly News
  • Rock Center
  • Meet the Press
  • Dateline
  • msnbc
  • Breaking News
  • Newsvine
  • Home
  • US
  • World
  • Politics
  • Business
  • Sports
  • Entertainment
  • Health
  • Tech
  • Science
  • Travel
  • Local
  • Weather
Advertise | AdChoices
  • Recommended: Customizing RavenDB: A simple RavenDB server bundle for replication conflict handling
  • Recommended: How we got started with RavenDB
  • Recommended: Raven DB: Lessons Learned: Caching Contexts
  • Recommended: Using WinDbg to troubleshoot high CPU in an ASP.Net MVC app


Musings on (some of the) software development at NBCNews.com

  • ↓ About this blog
  • ↓ Archives
    • Icons Email E-mail updates
    • Icons Feed Subscribe to RSS
  • 19
    Aug
    2011
    6:44pm, EDT

    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;
    2.  
    3. public AdminController(IDocumentStore store)
    4. {
    5.     this.store = store;
    6. }
    By Bryan Wheeler

     

    Pretty slick.

    1 comment

    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 tak …

    Show more
    Explore related topics: dependency-injection, ravendb, ninject, asp-net-mvc

Browse

  • ravendb,
  • featured,
  • service-bus,
  • skypad,
  • msmq,
  • performance,
  • wcf,
  • caching,
  • asp-net-mvc,
  • agile,
  • replication,
  • cache,
  • kanban,
  • feedback-loop,
  • technology,
  • dependency-injection,
  • ninject,
  • debugging,
  • elections,
  • givecamp,
  • charitable-giving
Also
Advertise | AdChoices

Bryan Wheeler

Bryan is the Director of Platform Development at msnbc.com.

Other stuff
In case it wasn't obvious, the content on this blog reflects my personal opinions. These opinions may or not be the same as my employer.

Archives

  • 2012
    • March (7)
    • February (3)
    • January (1)
  • 2011
    • December (2)
    • November (1)
    • August (3)
    • June (3)
    • May (1)
    • April (3)
    • March (3)
    • February (11)
    • January (2)
  • 2010
    • December (1)

Most Commented

    Other blogs

    • The Body Odd
    • Cosmic Log
    • Red Tape Chronicles
    • PhotoBlog
    • US News
    • Open Channel

    NBCNews.com top stories

    3147,10
    © 2013 NBCNews.com
    • About us
    • Contact
    • Help
    • Site map
    • Careers
    • Closed captioning
    • Terms & Conditions
    • Privacy policy
    • Advertise