关于将 Ninject 与 asp.net mvc 4 一起使用,我不知道如何处理 .net 中通过 google 提供的大量文档
首先,我想知道ASP.NET中是否需要控制器工厂。
另外,构造函数注入真的是我们可以使用 MVC 4 进行依赖项注入的唯一方法吗,因为当我将属性注入和方法注入与我的控制器一起使用时,它们似乎不起作用
我不是这方面的专家Ninject
但据我所知,我只是用它来链接我的DataSource Interface
and my EfDb Class
我的申请的其余部分。
如果您需要一本围绕真实应用程序构建的好书Ninject
try:
or
我通常关心的代码行很少
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return controllerType == null
? null
: (IController) ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
ninjectKernel.Bind<IDataSource>().To<EfDb>();
}
}
然后注册您的NinjectControllerFactory
in Global.asax.cs
with:
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
正如你所看到的,这个类使用Method Injection
using private void AddBindings()
。如果您正在关注,这会让事情变得非常容易Test Driven Development (TDD)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)