当我在 IOC (ninject) 上更改这行代码时,MVC 如何更改要使用的控制器构造函数?
我已经在 global.asax 文件中的项目上设置了 ninject...
protected void Application_Start()
{
RegisterDependencyResolver();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
private void RegisterDependencyResolver()
{
var kernel = new StandardKernel();
kernel.Bind<PteDotNetCore.IBlogManager>().To<PteDotNetCore.BlogManager>();
DependencyResolver.SetResolver(new PteDotNet.Resolution.PteDotNetDependencyResolver(kernel));
}
我有一个带有 2 个构造函数的控制器...
public CodeController()
{
}
public CodeController(IBlogManager injectedManager)
{
manager = injectedManager;
}
当我删除该行时:
kernel.Bind<PteDotNetCore.IBlogManager>().To<PteDotNetCore.BlogManager>();
从第一个代码块中添加该行时将使用我的默认构造函数然后使用采用该接口的构造函数。
请有人告诉我 MVC 做出了什么决定来选择构造函数?还不太清楚。
I have set up ninject on my project in the global.asax file...
protected void Application_Start()
{
RegisterDependencyResolver();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
private void RegisterDependencyResolver()
{
var kernel = new StandardKernel();
kernel.Bind<PteDotNetCore.IBlogManager>().To<PteDotNetCore.BlogManager>();
DependencyResolver.SetResolver(new PteDotNet.Resolution.PteDotNetDependencyResolver(kernel));
}
I have a controller with 2 consturctors....
public CodeController()
{
}
public CodeController(IBlogManager injectedManager)
{
manager = injectedManager;
}
When I remove the line:
kernel.Bind<PteDotNetCore.IBlogManager>().To<PteDotNetCore.BlogManager>();
from the first block of code my defaul constructor is used when the line is added then the constructor which takes the interface is used.
Please can someone tell me what decisions are being made by MVC to select the constructor? Its not quite clear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个决定实际上是由ninject做出的。对于构造函数注入,它会选择参数最多的一个,请参阅 wiki 了解其工作原理的详细信息
https://github.com/ninject/ninject/wiki/Injection-Patterns/1e462388cee1887a4bb90067cf334d91604f7ea8
The decision is actually made by ninject. For constructor injection it selects the one with the most parameters, see the wiki for details on exactly how this works
https://github.com/ninject/ninject/wiki/Injection-Patterns/1e462388cee1887a4bb90067cf334d91604f7ea8
控制器是使用控制器工厂构建的。通过设置 DependencyResolver,您可以告诉 MVC 使用 IoC 容器来构造控制器。
默认情况下没有 IoC,因此将使用默认的无参数构造函数。
当存在 IoC 容器 (Ninject) 时,控制器工厂会看到控制器构造函数具有参数。它将在 IoC 容器中查找以解决这些依赖关系并将实例传递给 arg 构造函数。
The controllers are built using a controller factory. By setting DependencyResolver, you are telling MVC to use your IoC container to construct controllers.
By default there is no IoC, and as such the default no-arg constructors will be used.
When there is an IoC container (Ninject), the controller factory sees that your controller constructors have args. It will look in your IoC container to resolve those dependencies and pass instances to the arg constructors.