Suppose I have 3 classes, Program
, A
(with dependencies D1
and D2
) and B
(with dependencies D3
and D4
). Program
initialises an IOC container and registers a bunch of types before creating an instance of A
.
class Program
{
static void Main(string[] args)
{
IOC ioc = new IOC();
//register types
A a = ioc.Resolve<A>();
}
}
At some point later on, a
needs to create an instance of B
, injecting its two dependencies into the constructor.
Question: How should a
resolve the dependencies of B
? I have been led to believe that passing the IOC container around i.e. injecting it into A
is the Service Locator pattern, which is bad. If B
created C
with its dependencies, B
would also need to have the container injected, and the container would be littered throughout my code. This sounds like a testing nightmare. Using a global static doesn't sound much better.