4 DI patterns

 

In this chapter

  • Composing object graphs with Composition Root
  • Statically declaring required Dependencies with Constructor Injection
  • Passing Dependencies outside the Composition Root with Method Injection
  • Declaring optional Dependencies with Property Injection
  • Understanding which pattern to use

Like all professionals, cooks have their own jargon that enables them to communicate about complex food preparation in a language that often sounds esoteric to the rest of us. It doesn’t help that most of the terms they use are based on the French language (unless you already speak French, that is). Sauces are a great example of the way cooks use their professional terminology. In chapter 1, we briefly discussed sauce béarnaise but we didn’t elaborate on the taxonomy that surrounds it.

A sauce béarnaise is really a sauce hollandaise where the lemon juice is replaced by a reduction of vinegar, shallots, chervil, and tarragon. Other sauces are based on sauce hollandaise, including Mark’s favorite, sauce mousseline which is made by folding whipped cream into the hollandaise.

Did you notice the jargon? Instead of saying, “carefully mix the whipped cream into the sauce, taking care not to collapse it,” we used the term folding. Instead of saying, “thickening and intensifying the flavor of vinegar,” we used the term reduction. Jargon allows you to communicate concisely and effectively.

4.1 Composition Root

4.1.1 How Composition Root works

4.1.2 Using a DI Container in a Composition Root

4.1.3 Example: Implementing a Composition Root using Pure DI

4.1.4 The apparent dependency explosion

4.2 Constructor Injection

4.2.1 How Constructor Injection works

4.2.2 When to use Constructor Injection

4.2.3 Known use of Constructor Injection

4.2.4 Example: Adding currency conversions to the featured products

4.2.5 Wrap-up

4.3 Method Injection

4.3.1 How Method Injection works

4.3.2 When to use Method Injection

4.3.3 Known use of Method Injection

4.3.4 Example: Adding currency conversions to the ProductEntity

sitemap