构造函数参数控制器不为ASP.NET MVC的一个DI容器 [英] Constructor parameters for controllers without a DI container for ASP.NET MVC

查看:96
本文介绍了构造函数参数控制器不为ASP.NET MVC的一个DI容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人对如何创建具有参数比使用依赖注入容器等控制器的任何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;
}

这篇关于构造函数参数控制器不为ASP.NET MVC的一个DI容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆