Ninject如何在ASP.NET MVC中创建控制器?

时间:2021-12-30 04:03:21

This may be stupid question, but I am looking at Ninject sources and don't see NInject registering its own controller factory. I also don't see any IControllerFactory class in Ninject.Web.Mvc assembly. Am I missing something? How does Ninject create controller and inject parameters into constructor?

这可能是一个愚蠢的问题,但我正在看Ninject来源,并没有看到NInject注册自己的控制器工厂。我也没有在Ninject.Web.Mvc程序集中看到任何IControllerFactory类。我错过了什么吗? Ninject如何创建控制器并将参数注入构造函数?

3 个解决方案

#1


12  

  1. Lets say we are looking for "/Task/Index".
  2. 让我们说我们正在寻找“/任务/指数”。
  3. Ninject MVC applications use now DefaultControllerFactory, the same as non-Ninject applications.
  4. Ninject MVC应用程序现在使用DefaultControllerFactory,与非Ninject应用程序相同。
  5. DefaultControllerFactory finds type for controller (TaskController).
  6. DefaultControllerFactory查找控制器的类型(TaskController)。
  7. DefaultControllerFactory has internal class called DefaultControllerActivator. DefaultControllerActivator has method called Create, which returns controller instance. DefaultControllerFactory asks DefaultControllerActivator for TaskController type instance.
  8. DefaultControllerFactory有一个名为DefaultControllerActivator的内部类。 DefaultControllerActivator具有名为Create的方法,该方法返回控制器实例。 DefaultControllerFactory向TaskController类型实例请求DefaultControllerActivator。
  9. DefaultControllerActivator.Create uses IDependencyResolver. This is where Ninject comes in. Since Ninject implements its own resolver and sets it at the start of application, he gets request for TaskController instance.
  10. DefaultControllerActivator.Create使用IDependencyResolver。这就是Ninject的用武之地。由于Ninject实现了自己的解析器并在应用程序启动时设置它,因此他获得了TaskController实例的请求。
  11. The rest is easy. Ninject finds constructor for this type, injects parameters, returns controller instance.
  12. 其余的很容易。 Ninject找到此类型的构造函数,注入参数,返回控制器实例。

#2


4  

MVC3 now recommends the usage of the IDependencyResolver interface instead of the good old IControllerFactory when dealing with DI. You can look at more details of this interface here.

MVC3现在建议在处理DI时使用IDependencyResolver接口而不是旧的IControllerFactory。您可以在此处查看此界面的更多详细信息。

This is the new Ninject class responsible for injecting the dependencies.

这是负责注入依赖项的新Ninject类。

#3


0  

Since controllers are concrete types, Ninject will do self bind. Below is a snippet from ninject.complex.com

由于控制器是具体类型,Ninject将自我绑定。以下是ninject.complex.com的片段

Bear in mind that only concrete types can be self-bound; abstract types and interfaces won't work. Also, if you request an instance of a type that can be self-bound, and there are no bindings defined for the type, Ninject will automatically create an implicit self-binding. It's up to you whether you want to define your bindings explicitly, or let Ninject figure it out.

请记住,只有具体类型可以自我约束;抽象类型和接口将无法正常工作。此外,如果您请求可以自我绑定的类型的实例,并且没有为该类型定义绑定,Ninject将自动创建隐式自绑定。您是否要明确定义绑定或让Ninject弄清楚它取决于您。

If you do need to inject parameters into the constructor. You can create a class inherits from INinjectModule and do the binding there.

如果确实需要将参数注入构造函数。您可以创建一个继承自INinjectModule的类并在那里进行绑定。

#1


12  

  1. Lets say we are looking for "/Task/Index".
  2. 让我们说我们正在寻找“/任务/指数”。
  3. Ninject MVC applications use now DefaultControllerFactory, the same as non-Ninject applications.
  4. Ninject MVC应用程序现在使用DefaultControllerFactory,与非Ninject应用程序相同。
  5. DefaultControllerFactory finds type for controller (TaskController).
  6. DefaultControllerFactory查找控制器的类型(TaskController)。
  7. DefaultControllerFactory has internal class called DefaultControllerActivator. DefaultControllerActivator has method called Create, which returns controller instance. DefaultControllerFactory asks DefaultControllerActivator for TaskController type instance.
  8. DefaultControllerFactory有一个名为DefaultControllerActivator的内部类。 DefaultControllerActivator具有名为Create的方法,该方法返回控制器实例。 DefaultControllerFactory向TaskController类型实例请求DefaultControllerActivator。
  9. DefaultControllerActivator.Create uses IDependencyResolver. This is where Ninject comes in. Since Ninject implements its own resolver and sets it at the start of application, he gets request for TaskController instance.
  10. DefaultControllerActivator.Create使用IDependencyResolver。这就是Ninject的用武之地。由于Ninject实现了自己的解析器并在应用程序启动时设置它,因此他获得了TaskController实例的请求。
  11. The rest is easy. Ninject finds constructor for this type, injects parameters, returns controller instance.
  12. 其余的很容易。 Ninject找到此类型的构造函数,注入参数,返回控制器实例。

#2


4  

MVC3 now recommends the usage of the IDependencyResolver interface instead of the good old IControllerFactory when dealing with DI. You can look at more details of this interface here.

MVC3现在建议在处理DI时使用IDependencyResolver接口而不是旧的IControllerFactory。您可以在此处查看此界面的更多详细信息。

This is the new Ninject class responsible for injecting the dependencies.

这是负责注入依赖项的新Ninject类。

#3


0  

Since controllers are concrete types, Ninject will do self bind. Below is a snippet from ninject.complex.com

由于控制器是具体类型,Ninject将自我绑定。以下是ninject.complex.com的片段

Bear in mind that only concrete types can be self-bound; abstract types and interfaces won't work. Also, if you request an instance of a type that can be self-bound, and there are no bindings defined for the type, Ninject will automatically create an implicit self-binding. It's up to you whether you want to define your bindings explicitly, or let Ninject figure it out.

请记住,只有具体类型可以自我约束;抽象类型和接口将无法正常工作。此外,如果您请求可以自我绑定的类型的实例,并且没有为该类型定义绑定,Ninject将自动创建隐式自绑定。您是否要明确定义绑定或让Ninject弄清楚它取决于您。

If you do need to inject parameters into the constructor. You can create a class inherits from INinjectModule and do the binding there.

如果确实需要将参数注入构造函数。您可以创建一个继承自INinjectModule的类并在那里进行绑定。