Chapter 3. Investigating DI

 

This chapter covers:

  • Learning about injection by setter and constructor
  • Investigating pros and cons of injection idioms
  • Identifying pitfalls in object graph construction
  • Learning about reinjecting objects
  • Discovering viral injection and cascaded object graphs
  • Learning techniques to inject sealed code

“The best way to predict the future is to invent it.”

Alan Kay

Previously we discussed two methods of connecting objects with their dependencies. In the main we have written classes that accept their dependencies via constructor. We have also occasionally used a single-argument method called a setter method to pass in a dependency. As a recap, here are two such examples from chapter 1:

public class Emailer {
private SpellChecker spellChecker;

public Emailer(SpellChecker spellChecker) {
this.spellChecker = spellChecker;
}
}

The same class accepting dependencies by setter:

public class Emailer {
private SpellChecker spellChecker;

public void setSpellChecker(SpellChecker spellChecker) {
this.spellChecker = spellChecker;
}
}

These are two common forms of wiring. Many dependency injectors also support other varieties and bias toward or away from these idioms.

3.1. Injection idioms

 

3.2. Choosing an injection idiom

 
 
 
 

3.3. Not all at once: partial injection

 
 

3.4. Injecting objects in sealed code

 
 
 

3.5. Summary

 
 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest