Steps to Get MVC running with Ninject

  1. enable package restore on solution
  2. Nuget Ninject.MVC3 (pre-release available?)
    * adds Ninject, Ninject.Web.Common, Ninject.Web.Mvc
    * App_Start/NinjectWebCommon.cs -> has WebActivator.PreApplicationStartMethod attribute -> runs at startup
  3. add interface and class for testing (IBikeManager and BikeManager)
  4. add controller constructor to HomeController that takes in the interface
    - run website => Error
    "Error activating IBikeManager
    No matching bindings are available, and the type is not self-bindable.
    Activation path:
    2) Injection of dependency IBikeManager into parameter bikeManager of constructor of type HomeController
    1. Request for HomeController"
  5. in NinjectWebCommon, register the services in the RegisterServices Method
  6. kill the IISExpress instance (this only runs on app startup, so you’ll have to do this each time you register a new service), restart app (ctrl + F5)
  7. HomeController now gets loaded with the IBikeManager in the constructor (set breakpoint and start with F5 to prove it)

That’s all there is to using Ninject as you IoC container and constructor injection (Dependency Injection aka DI) working with MVC (3, 4, or 5).

CodeProject

This article is part of the GWB Archives. Original Author: Aligned

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.