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:
var kernel = new StandardKernel();
var scanner = new AssemblyScanner();
scanner.FromAssembliesMatching("Basketball.Logic.dll"); // Services classes here
scanner.FromAssembliesMatching("Basketball.Data.dll"); // Repositories here
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!