2 回答
TA贡献1775条经验 获得超8个赞
这与依赖注入没有多大关系;什么构造函数
public class TestController
{
private IUnitOfWork unitOfWork;
public TestController(IUnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
}
}
它只是简单地获取一个对象的实例IUnitOfWork并将其存储在它的 field 中unitOfWork。
所以,你可能(但不应该)这样称呼它:
// Returns an instance of IUnitOfWork.
IUnitOfWork mySpecificUnitOfWorkInstance = this.GetUnitOfWork();
// Now you pass that exact instance to the TestController so that it can do stuff with it.
TestController testController = new TestController(mySpecificUnitOfWorkInstance);
更简单的例子可能(没有依赖注入、存储库模式等可能令人困惑的概念):
public class NumberHolder
{
private int number = 0;
public NumberHolder(int number)
{
this.number = number;
}
}
如果你这样称呼它
NumberHolder foo = new NumberHolder(42);
你实际上传递42给NumberHolder.
- 2 回答
- 0 关注
- 172 浏览
添加回答
举报