Wednesday, 15 June 2011

Ninject and ASP.NET MVC 3 - Surely it can't be that easy?

Over the past couple of weeks I've started to rewrite a project I wrote a few years ago using Sharp Architecture and ASP.NET MVC 1. I wanted to update to ASP.NET MVC 3 and remove the dependency on Sharp Architecture which would force me to implement a lot more functionality myself, and therefore (hopefully) increase my understanding. One of the tasks I've been putting off is choosing a dependency injection container. I've had a little experience of Castle and Unity and they both seemed fine, but to be honest, I wanted to be really lazy and implement dependency injection with the least amount of effort. Enter Ninject. The only reason I started reading about Ninject was because I liked the name. But, turns out my random affinity for something I knew nothing about based on the name alone turned up trumps. 

At lunchtime today I read some of the Ninject documentation. Turns out there is a Ninject extension for MVC 3 and a convention extension which should, in theory, allow for code without loads of bind commands. Sounds promising. The good news is that the Ninject documentation is very good and the MVC 3 extension documentation is good. The bad news is that the convention extension documentation is seemingly non-existent. I certainly couldn't find much useful documentation anyway. But, I gave it a shot anyway, and in about an hour or so I had functioning dependency injection with minimal code. It was so simple I thought I must have made a mistake. But no, it works. I'm not sure it was the best solution but I don't care, it works. 

Before I continue it's worth me explaining some my basic project structure. It's nothing fancy. I've separated data access, service and web stuff into separate projects. All my classes follow a convention like SomeController, SomeService, SomeRepository, ISomeService, etc. All the classes has parameterless constructors. So it's helpfully simple. Anyway, here is what I did.

Firstly I installed Ninject and the Ninject MVC 3 extension using Nuget as described here.

I then installed the Ninject conventions extension using Nuget.

In the NinjectMVC3.cs file created by the Ninject MVC 3 plugin I changed the CreateKernal method to the following:

private static IKernel CreateKernel()
{
    var kernel = new StandardKernel();

    var scanner = new AssemblyScanner();
    scanner.FromAssembliesMatching("Basketball.Logic.dll"); // Services classes here
    scanner.FromAssembliesMatching("Basketball.Data.dll");  // Repositories here
    scanner.BindWithDefaultConventions();

    kernel.Scan(scanner);

    return kernel;

And that's it. It worked. An instance of my repository class was injected into my service class and an instance of my service class was injected into my controller class. High five! In addition when I add a new controller/service/repository there's no need to add any further Ninject binding code. Brilliant!

No comments:

Post a comment