Abstract factory pattern on top of IoC?

As you have already figured out, Dependency Injection (DI) itself is only a collection of patterns and techniques.

At the root of the application we wire up all necessary object graphs. This place is called the Composition Root, and we can use a DI Container to do this wiring for us, or we can do it manually (Pure DI).

The point is that there’s only one place in your application where there’s a strong reference to a particular piece of technology (your DI Container). The rest of the app is blissfully unaware of how the object graph was wired up – all that matters is that all required dependencies were correctly injected (and you can use Constructor Injection with Null Guards to guarantee that this is so).

The Abstract Factory pattern is a very useful pattern when it comes to DI. In essence, use Abstract Factory when:

  • You need to supply one or more parameters only known at run-time before you can resolve a dependency.
  • The lifetime of the dependency is conceptually shorter than the lifetime of the consumer.

Examples and more information is available here:

Leave a Comment