Suppose I have MyClass
with a very simple dependency. It uses constructor injection.
public class MyClass : IMyClass
{
private IA A;
public MyClass(IA a)
{
A = a;
}
}
This code get shipped and some other developers are using it.
At some point in the future I need to add in a second dependency, on IB.
How should I add this dependency on IB?
Some options, and why I disapprove:
- Just add it to the constructor
- Braking change.
- Inheritance or the decorator pattern.
- Extra classes and code bloat.
- New class will have boilerplate code passing around the IA.
- I have no guarantee that other developers will switch to using the new class.
- Use a service locator to populate the new IB field.
- I'm now using two different types of injection for extra confusion.
It seems to me that constructor injection is just not easily extensible.
private IB B = new B();
, doesn't break anything. Only when we try to use constructor injection do we see a breaking change. Do you agree?