Dependency Inversion Using
Ports and Adapters
● The problem
● The idea
● The aims
● Evolution of an existing application
● The concepts
● Show me the code
Over time core business logic of an application becomes tightly coupled with
various libraries, frameworks and technologies making it harder to evolve and test.
● Long lasting product that needs to keep up with changing customer needs
and evolution of technology.
● Ability to change core logic confidently with 100% unit test coverage.
● Ability to switch third party codes easily to avoid obsolete code due to
Ports and Adapters Architecture (Alistair Cockburn)
"Allow an application to equally be driven by users, programs, automated test or
batch scripts, and to be developed and tested in isolation from its eventual run-
time devices and databases."
This architecture is also known as:
● Hexagonal Architecture
● Plug-in Architecture
● Decoupling application's core business logic from the services it uses.
● Allowing different services to be plugged in.
● Allowing the application to be run without these services.
● Allowing it to become framework agnostic.
An existing application (order processing)
Here core business logic is coupled with UI
and various libraries, frameworks and
● Leakage of external libraries and
technologies in the core logic by direct
● Leakage of core logic in the UI which is
not reusable in other UI.
● Leakage of the UI into the core logic.
Evolution using ports and adapters
Primary/Driving Adapters Secondary/Driven Adapters
● Core as the central artefact of a system.
● All input and output reaches/leaves the core through a port that isolates the
core from external tools, technologies and delivery mechanisms.
● The core should have no knowledge of who/what is sending input or receiving
● This is intended to provide some protection against the evolution of
The concepts (an example)
Show me the code