1

What is the best way to use Ninject with MVC 3 and how ?

It is using a controller factory ? or using NinjectHttpApplication ?

How to get ninject for MVC 3 ? Ive looked around but i can't seem to figure out how to get the required ..mvc.dll

Any exemples would be helpful to me and others.

Thanks!

Rushino
  • 9,415
  • 16
  • 53
  • 93

2 Answers2

7

The best and simplest way to get Ninject for MVC 3 is adding the NuGet package Ninject.MVC3 using the NuGet Package Manager from within Visual Studio.

This will set up everything for you, add an App_Start folder to your application which contains NinjectMVC3.cs. At this point everything is already hooked up as Controller Factory thanks to WebActivator - you can just add your bindings in RegisterServices() or of course put them in a separate module.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • What the avantage in putting them in seperate module, also what a module ? – Rushino Sep 17 '11 at 15:12
  • 1
    @Rushino: Ninject modules allow you to have a set of bindings encapsulated in their own class - for a complete explanation see this SO thread: http://stackoverflow.com/questions/2056409/what-is-the-intention-of-ninject-modules – BrokenGlass Sep 17 '11 at 16:08
1

For me the best way is to exted from NinjectHttpApplication in the global.asax and then override the IKernel CreateKernel() method

public class MvcApplication : NinjectHttpApplication {
  ...
  ...

  protected override IKernel CreateKernel() {
    var kernel = new StandardKernel();
    kernel.Load(Assembly.GetExecutingAssembly());
    // Register services with Ninject DI Container    
    kernel.Bind<IFileSystemService>().To<FileSystemService>();
    return kernel;
  }

  ...
}
Marco Staffoli
  • 2,475
  • 2
  • 27
  • 29