如果我有 10 个依赖项,我需要注入并且不想在构造函数中有 10 个参数,我应该使用哪种注入模式?
public class SomeClass
{
private IDependency1 _dependency1;
private IDependency2 _dependency2;
private IDependency3 _dependency3;
//...
}
我应该使用setter方法注入吗?
public class SomeClass
{
private IDependency1 _dependency1;
private IDependency2 _dependency2;
private IDependency3 _dependency3;
//...
[Inject]
public void SetDependency1(IDependency1 dependency1)
{
_dependency1 = dependency1;
}
//...
}
还是属性注入?
public class SomeClass
{
[Inject]
public IDependency1 Dependency1 { private get; set; }
[Inject]
public IDependency2 Dependency2 { private get; set; }
[Inject]
public IDependency3 Dependency3 { private get; set; }
//...
}
根据 Ninject wiki,只写像上面这样的属性被认为是不好的做法,但它与上面的 setter 方法注入不一样,只是代码更少吗?
对于这种情况,哪种模式最有意义?