构造函数参数控制器不为ASP.NET MVC的一个DI容器不为、控制器、容器、函数

2023-09-02 10:50:02 作者:一身傲骨怎敢服输

有没有人对如何创建具有参数比使用依赖注入容器等控制器的任何code的例子吗?

Does anyone have any code examples on how to create controllers that have parameters other than using a Dependency Injection Container?

我看到很多的样品使用的容器,如StructureMap,但没有什么,如果你想通过自己的依赖类。

I see plenty of samples with using containers like StructureMap, but nothing if you wanted to pass in the dependency class yourself.

推荐答案

您可以用可怜的人的依赖注入:

You can use poor-man's dependency injection:

public ProductController() : this( new Foo() )
{
  //the framework calls this
}

public ProductController(IFoo foo)
{
  _foo = foo;
}
 
精彩推荐
图片推荐