public class MyClass
{
private IVolatileDependency1 _one;
private StableDependency _stable;
public MyClass(IVolatileDependency1 one, IVolatileDependency2 two)
{
_one = one;
_stable = new StableDependency(two)
}
}
public class StableDependency
{
private IVolatileDependency2 _two;
public StableDependency(IVolatileDependency2 two)
{
_two = two;
}
}
public class MyClass
{
private IVolatileDependency1 _one;
private StableDependency _stable;
public MyClass(IVolatileDependency1 one, IVolatileDependency2 two)
{
_one = one;
_stable = new StableDependency(two)
}
}
public class StableDependency
{
private IVolatileDependency2 _two;
public StableDependency(IVolatileDependency2 two)
{
_two = two;
}
}
EDIT: Changed VolatileDependency1 and VolatileDependency2 to interfaces to clarify that they are intended to be satisfied by multiple possible implementations (LSP-compatible).
EDIT 2: Thinking on this some more, and reading KeithS's answer and default.kramer's comment below, make me realize that maybe I shouldn't have named StableDependency as I did, because I don't think it's a dependency in the first place. It's not something I ever expect to have multiple implementations for; it's merely an implementation detail that MyClass uses composition to contain a StableDependency, so MyClass's knowledge of how to construct a StableDependency isn't a problem I don't think. The big question is, StableDependency does have a dependency on IVolatileDependency2, so how do I inject that dependency? Do I reason that for all intents and purposes MyClass has a dependency on IVolatileDependency2, and the fact that it just passes that dependency down to StableDependency is an implementation detail unrelated to DI?