liveBook · Manning

Dependency inversion

As explained in Grokking Software Architecture

The Dependency Inversion Principle states that high-level and low-level modules should depend on abstractions, while abstractions should not depend on implementation details.

A coach who builds a game plan around specific players must rewrite it whenever the lineup changes. A plan built around roles can accommodate different players who fulfill those roles. Dependency inversion applies the same idea to software: high-level modules and low-level modules both depend on abstractions, rather than high-level logic depending directly on concrete details. section 3.4.5

The abstraction defines a shared contract; concrete implementations fulfill it. The contract should not depend on those implementation details. This keeps business rules or orchestration loosely coupled to details such as database access, making changes and isolated tests easier. section 3.4.5

Listing 3.11 “Before”: Violating DIP
// Low-level modules (concrete players)
class Forward { 
    attack() { console.log(“Forward is attacking.”); } 
}
class Midfielder {
    controlMidfield() { 
          console.log(“Midfielder controlling the game.”);
    }
}
 
// High-level module (the Coach)
class Coach { #A
     constructor() {    
         this.forward = new Forward(); #B
         this.midfielder = new Midfielder();#B
     }
     executeGamePlan() {
          this.forward.attack();
          this.midfielder.controlMidfield();
     }
}

Before inversion, the coach constructs specific players and calls their individual methods. section 3.4.5

Listing 3.12 “After”: Following DIP
 
// Low-level modules (depend on the abstraction)
class Forward {
    performAction() {
        console.log(“Forward is attacking.”);
    }
}
 
class Midfielder {
    performAction() { 
        console.log(“Midfielder is controlling the game.”);
    }
}
 
class Winger { #A
    performAction() { 
        console.log(“Winger is running down the sideline.”); #A
    }
} #A
 
// High-level module (also depends on abstractions)
class Coach {
    constructor(players) { this.team = players; } #B
    executeGamePlan() {
          for (const player of this.team) {
               player.performAction();
          }
     }
}

After inversion, the coach receives players and uses their shared performAction contract. A new player can join without changing the game plan. section 3.4.5

Injecting a logger

An order service that calls a static file logger is tied to that logging implementation. Instead, give the service a logger through its constructor and have it use the logging contract. A file logger can later be replaced with a cloud logger where services are registered, while a test can supply a fake logger without writing a file. Dependency injection supplies the dependency; the shared abstraction is what lets the service avoid depending on its concrete details. section 4.5.2 section 4.5.3

The tradeoff is a little setup complexity: a contract and a way to supply an implementation. The payoff is less coupling between business logic and technical concerns. At architectural scale, ports and adapters apply the same idea to protect core logic from dependencies on databases and other external details. section 4.5.3 section 5.1

Watch for concrete dependencies hidden inside important workflows. A checkout flow that directly calls a particular HTTP library and URL is hard to replace or mock. Inverting that dependency helps separate the external service from business logic, but network failures and timeouts still need explicit attention. section 10.4.1

How Grokking Software Architecture covers it

How deep it goes
Explains dependency direction through before-and-after implementations and applies it to logging and architectural boundaries.
Examples
Uses a coach and interchangeable players, an injected order-service logger, and a checkout workflow with a hardcoded HTTP dependency.
What you can do after
Apply dependency inversion by defining a shared contract and supplying an implementation to high-level logic.
How it connects
Connects dependency inversion to loose coupling, separation of concerns, dependency injection, layered architecture, and hexagonal architecture.

Also explained in

Compare how 8 books explain dependency inversion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.