Rushino
Rushino

Reputation: 9495

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

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!

Upvotes: 1

Views: 1204

Answers (2)

Marco Staffoli
Marco Staffoli

Reputation: 2496

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;
  }

  ...
}

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160902

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.

Upvotes: 7

Related Questions