开发者

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

开发者 https://www.devze.com 2023-04-05 06:21 出处:网络
What is the best way to use Ninject with MVC 3 and how ? It is using a controller factory ? or using NinjectHttpApplic开发者_运维问答ation ?

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

It is using a controller factory ? or using NinjectHttpApplic开发者_运维问答ation ?

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!


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.


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

  ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号