Asp.net 太过分了?
我想将Ninject for IoC用于ASP.NET MVC web应用程序。我有以下代码示例:Asp.net 太过分了?,asp.net,ninject,Asp.net,Ninject,我想将Ninject for IoC用于ASP.NET MVC web应用程序。我有以下代码示例: // resides in a MyApplication.Web assembly public class SomeController { ... public ActionResult myControllerAction() { Service service = <--- should this be instantiated by Ninject? s
// resides in a MyApplication.Web assembly
public class SomeController {
...
public ActionResult myControllerAction() {
Service service = <--- should this be instantiated by Ninject?
service.doSomeLogic();
...
}
}
// resides MyApplication.Common assembly
public class Service {
public void doSomeLogic() {
...
}
}
//驻留在MyApplication.Web程序集中
公共类控制器{
...
公共行动结果MyControlleration(){
Service Service=正在创建的服务不需要依赖于Ninject。只有web项目需要它。您可能应该在控制器上使用构造函数注入(和服务,如果它也有依赖项)。您可以在global.asax中的web项目中连接它们,或者更可能是从中调用的配置类。请使用,按照来配置您的服务
public class SomeController {
private readonly Service _service;
public SomeController(Service service)
{
_service = service;
}
public ActionResult myControllerAction() {
_service.doSomeLogic();
...
}
}
正在创建的服务不需要依赖于Ninject。只有web项目需要它。您可能应该在控制器(和服务,如果它也有依赖项)上使用构造函数注入。您可以在global.asax中的web项目中连接它们,或者更可能是从中调用的配置类。请使用,按照来配置您的服务
public class SomeController {
private readonly Service _service;
public SomeController(Service service)
{
_service = service;
}
public ActionResult myControllerAction() {
_service.doSomeLogic();
...
}
}
我非常喜欢这个答案,是否有一个具体的例子说明如何在全局asax中进行构造函数注入的连接?我非常喜欢这个答案,是否有一个具体的例子说明如何在全局asax中进行构造函数注入的连接?