liveBook · Manning

Dependency inversion

As explained in Clean Applications with Hexagonal Architecture

A design principle stating that high-level and low-level components should depend on abstractions rather than concrete implementation details.

When business logic depends directly on a database, framework, or transport model, an infrastructure change can ripple through the core. Dependency inversion protects that core by making both the business logic and external implementations depend on abstractions rather than concrete details. section 1.0 section 1.1.2

In hexagonal architecture, the core owns its port interfaces. A handler uses an output port to express what it needs; an external adapter implements that port. The adapter depends inward on the core’s contract, not the other way around. The same boundary applies to input ports: a REST adapter can call a use-case interface without depending on its handler implementation. section 1.1.2 section 9.3.2

Listing 1.2 The core never knows about the specific details of the adapters
public class ArticleService { // #A
    private final JpaArticleRepository repo; // can leak concrete class
}
 
package application.core.domain; // #B
public interface ArticleRepository { // #C
    Either<Error, Void> save(Article article);
    Either<Error, ArticleDTO> findById(ArticleId id);
}
 
package application.core.handler;
public class CreateArticleHandler { // #D
    private final ArticleRepository repo;   // interface defined by domain
}

Compare a service coupled to a concrete JPA repository with a handler that depends on a core-defined repository interface. section 1.1.2

A replaceable repository adapter

CreateArticleHandler depends on the core’s ArticleRepository interface. JpaArticleRepositoryAdapter implements that interface and contains the JPA details. A test can inject a mock repository; a different adapter can replace the JPA implementation without changing the handler’s business logic. section 1.1.2 section 2.3.1

Design ports around domain needs, not infrastructure methods, and keep business logic out of adapters. Enforce the dependency boundary with architectural tests such as ArchUnit so that framework and infrastructure dependencies do not creep back into the core. section 2.5 section 1.2.3

How Clean Applications with Hexagonal Architecture covers it

How deep it goes
Explains port ownership and dependency direction through repository and use-case interfaces, with implementation and testing consequences.
Examples
Uses an article-creation handler, a repository interface, mock and JPA adapters, and a REST-facing use-case interface.
What you can do after
Evaluate dependency boundaries by identifying core-owned ports, adapter dependencies, and leaks of business logic or infrastructure concerns.
How it connects
Connects ports and adapters, hexagonal architecture, layered architecture, and architectural testing.

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 Clean Applications with Hexagonal Architecture. The section links point to the parts of the book it draws on.