epa*_*ulk 0 asp.net-mvc dependency-injection ninject
我做了我的研究,但是我找不到任何我想做的具体例子.
我成功地将Ninject实现到了我的MVC项目中.一切都很完美.但是,我想做最后一步.
到现在为止,我一直在这样工作(正常的DI模式):
public class myController : Controller
{
private iMyInterface myRepository;
public myController(iMyInterface myRepository)
{
this.myRepository = myRepository;
}
public ActionResult list(){
return view(myRepository.getMyList())
}
// rest o the code ...
}
Run Code Online (Sandbox Code Playgroud)
我的问题是; 有办法做这样的事情吗?(存储库"Generator")
public class myController : Controller
{
private iMyInterface myRepository = specialClass.GetMyRepository();
public ActionResult list(){
return view(myRepository.getMyList()) }
// rest o the code ...
}
Run Code Online (Sandbox Code Playgroud)
我知道我正在写一个无意义的代码,但我的想法是能够做类似的事情.
有什么建议?
我不知道specialClass应该是什么,但这看起来很像Service Locator反模式.
通过构造函数注入存储库的第一个选项更好,因为它为您提供了更多选项,包括使用specialClass:
var controller = new myController(specialClass.GetMyRepository());
Run Code Online (Sandbox Code Playgroud)
我不知道Ninject,但是大多数DI容器都提供了一个选项,可以将接口映射到返回接口实例的方法调用.
| 归档时间: |
|
| 查看次数: |
296 次 |
| 最近记录: |