liveBook · Manning

Dependency injection

As explained in Simple Object-Oriented Design: Create clean, maintainable applications

A dependency injection framework is a software framework that constructs objects and supplies their declared dependencies according to configuration or conventions.

Dependency injection supplies a class’s collaborators from outside, usually through its constructor, instead of letting the class instantiate them directly. Its immediate payoff is visible dependencies: a reader can see what a class needs at the point it is created. section 4.4 section 2.1.5

Use it to keep responsibilities narrow and replaceable. An import service can receive an EmployeeRepository and EmployeeImportCSVParser, then focus on coordinating imports rather than configuring parsing. A bot adapter can receive its SDK client instead of constructing one while sending a message. section 2.1.5 section 4.2.2

Constructor injection

A class that accepts a parser and repository can parse input, find an employee, and choose create or update behavior without owning parser construction. Passing alternative implementations at runtime lets the same class work with different bots or user directories; mocks can replace costly or external collaborators in isolated tests. section 2.1.5 section 4.4

Injection does not eliminate coupling; it makes coupling easier to locate and control. Static calls can hide dependencies throughout a class, while constructor parameters expose them. Performance objections are usually not decisive outside exceptionally large-scale systems, but the technique should not become an excuse for an unnecessarily complex graph. section 4.4 section 4.4.3

Figure 4.6 The dependency graph of the MessageSender class

Inspect the MessageSender dependency graph to see how constructor-injected collaborators require their own dependencies and form a larger object graph. section 4.4.4

For deep graphs, dependency injection frameworks such as Spring or Guice can construct collaborators automatically. For straightforward applications, especially small command-line tools, factory classes may be a simpler choice. Choose the approach whose downsides remain controlled. section 4.4.3

How Simple Object-Oriented Design: Create clean, maintainable applications covers it

How deep it goes
Explains constructor injection, dependency graphs, testing benefits, performance objections, and framework-versus-factory tradeoffs without detailing framework configuration.
Examples
Uses ImportEmployeesService, an SDKBot wrapping ChatBotV1, and the MessageSender dependency graph.
What it assumes
Assumes familiarity with classes, constructors, interfaces, and collaborators rather than teaching object-oriented fundamentals from zero.
What you can do after
Apply it via constructors, frameworks, or factories to substitute implementations and isolate collaborators in tests.
How it connects
Connects dependency injection with dependency management, dependency graphs, interfaces, static methods, factories, and test mocks.

Also explained in

Compare how 21 books explain dependency injection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Simple Object-Oriented Design: Create clean, maintainable applications. The section links point to the parts of the book it draws on.