12 DI Container introduction


In this chapter

  • Using configuration files to enable late binding
  • Explicitly registering components in a DI Container with Configuration as Code
  • Applying Convention over Configuration in a DI Container with Auto-Registration
  • Choosing between applying Pure DI or using a DI Container

When I (Mark) was a kid, my mother and I would occasionally make ice cream. This didn’t happen too often because it required work, and it was hard to get right. Real ice cream is based on a crème anglaise which is a light custard made from sugar, egg yolks, and milk or cream. If heated too much, this mixture curdles. Even if you manage to avoid this, the next phase presents more problems. Left alone in the freezer, the cream mixture crystallizes, so you have to stir it at regular intervals until it becomes so stiff that this is no longer possible. Only then will you have a good, homemade ice cream. Although this is a slow and labor-intensive process, if you want to—and you have the necessary ingredients and equipment—you can use this technique to make ice cream.

Today, some 35 years later, my mother-in-law makes ice cream with a frequency unmatched by myself and my mother at much younger ages—not because she loves making ice cream, but because she uses technology to help her. The technique is still the same, but instead of regularly taking out the ice cream from the freezer and stirring it, she uses an electric ice cream maker to do the work for her (see figure 12.1).

12.1 Introducing DI Containers

12.1.1 Exploring containers’ Resolve API

12.1.2 Auto-Wiring

12.1.3 Example: Implementing a simplistic DI Container that supports Auto-Wiring

12.2 Configuring DI Containers

12.2.1 Configuring containers with configuration files

12.2.2 Configuring containers using Configuration as Code

12.2.3 Configuring containers by convention using Auto-Registration

12.2.4 Mixing and matching configuration approaches

12.3 When to use a DI Container

12.3.1 Using third-party libraries involves costs and risks

12.3.2 Pure DI gives a shorter feedback cycle

12.3.3 The verdict: When to use a DI Container