concept DI Containers in category dependency injection

appears as: DI Containers, DI Containers
Dependency Injection

This is an excerpt from Manning's book Dependency Injection.

A DI Container is a software library that provides DI functionality and automates many of the tasks involved in Object Composition, Interception, and Lifetime Management. DI Containers are also known as Inversion of Control (IoC) containers. Up until this point, we touched on the subject of DI Containers only gently. This is deliberate because, as we explained in chapter 1, a DI Container is a useful, but optional, tool. We postpone a detailed discussion about DI Containers until part 4 because we feel that teaching you about the set of principles and patterns that DI consists of, as well as the existing code smells and anti-patterns, is more important.

We build applications both with and without DI Containers, and you should be able to do so too. And yet, we feel that it would be counterproductive if you started using a DI Container without the knowledge presented in parts 2 and 3. On the other hand, after you understand the principles and practices, using a DI Container primarily consists of getting acquainted with its API. At this point, it’s only important to get a broad sense of what a DI Container is and how it can help you.

When the first edition of this book came out, we used DI Containers exclusively in all the applications we built. Although we knew that applying DI without a DI Container was possible, we thought that it was never practical. Our ideas about this have changed, and that’s why we now focus even more on the patterns and techniques behind DI.

In section 12.2, we talked about the different configuration methods of DI Containers and how the use of Auto-Registration can increase maintainability of your Composition Root. But the use of DI Containers comes with additional costs and disadvantages over Pure DI. Most, if not all, DI Containers are open source, so they’re free in a monetary sense. But because developer hours are typically the most expensive part of software development, anything that increases the time it takes to develop and maintain software is a cost, which is what we’ll talk about here.

In this section, we’ll compare the advantages and disadvantages, so you can make an educated decision about when to use a DI Container and when to stick to Pure DI. Let’s start with an often overlooked aspect of using libraries such as DI Containers, which is that they introduce costs and risks.