【发布时间】:2012-03-08 20:08:09
【问题描述】:
我有一堆具有一组依赖项的类。这个项目的依赖注入将是多余的,所以目前我们在许多情况下都有以下模式:
public MyClass() : this(null, null) {}
public MyClass(Dependancy x, Dependancy y)
{
this.x = x ?? new Dependancy();
this.y = y ?? new Dependancy();
}
我不喜欢这段代码,但我不完全确定为什么。一个原因是它会修改传入的参数,另一个是我可能希望参数为空,并保持为空。
是否有充分的理由避免/使用这种模式或任何其他模式,或者它基本上只是个人喜好?
【问题讨论】:
标签: c# dependency-injection constructor dependencies