Sunday, October 2, 2011

Ninjecting MVC3

Dabbling with a new MVC3 side-project, I chose Ninject to handle dependency injection. The creative project name might have something to do with that decision. So far, I am really impressed. Setting up Ninject is a breeze, and the framework does a really good job of not getting in your way.

In this post, I will walk you through a simple MVC3 project using Ninject.

Adding Ninject

Adding Ninject to your MVC3 project is simple enough.

Open the Nuget console and type 'Install-Package Ninject.MVC3'. This will make Nuget go to work, fetching the NinjectMVC3 package and its dependencies.
Attempting to resolve dependency 'Ninject (= && <'.
Attempting to resolve dependency 'WebActivator (= 1.4)'.
Attempting to resolve dependency 'Microsoft.Web.Infrastructure (='.
Successfully installed 'Ninject'.
Successfully installed 'Microsoft.Web.Infrastructure'.
Successfully installed 'WebActivator 1.4.4'.
Successfully installed 'Ninject.MVC3'.
Successfully added 'Ninject' to PrettyDIBaby.
Successfully added 'Microsoft.Web.Infrastructure' to PrettyDIBaby.
Successfully added 'WebActivator 1.4.4' to PrettyDIBaby.
Successfully added 'Ninject.MVC3' to PrettyDIBaby.
Making up some dependencies

Let's add an ITimeService to our models.
public interface ITimeService
    DateTime GetCurrentDateTime();
The implementation could look like this.
public class TimeService : ITimeService
    public DateTime GetCurrentDateTime()
        return DateTime.Now;
Now, we can make our HomeController depend on the ITimeService by adding it as a constructor argument.
public class HomeController : Controller
    private ITimeService _timeService;

    public HomeController(ITimeService timeService)
        _timeService = timeService;
The Index action on our controller will make use of this service to display the date on the Index view.
public ActionResult Index()
    ViewData["Time"] = _timeService.GetCurrentDateTime();

    return View();
Binding them dependencies

So far we have added Ninject to our project, added a silly ITimeService, and made our HomeController dependant on that service.

The only thing that's left for us to do, is binding the ITimeService interface to the TimeService implementation.

If you look in your project root, you will find an App_Start folder, containing a NinjectMVC3 class. All of this was added by Nuget for us. The NinjectMVC3 class is used to initialize and shut down Ninject using the bootstrapper. WebActivator makes sure that this happens when the application starts and stops. When the Ninject bootstrapper is initialized, we need to bind our dependencies.

In our example, I modified the RegisterServices method to look like this.
private static void RegisterServices(IKernel kernel)
Finishing strong

Et voila.

There is no need to mark your dependencies in the controller, nor is there any need to resolve your dependencies through the dependency container. Ninject will automagically resolve your dependencies.

You can download the source here.