This article is for sharing my understanding about Ports & Adapters pattern (also known as Hexagonal Architecture), hoping it can be helpful to someone interested in this subject.
Documented in 2005 by Alistair Cockburn, Hexagonal Architecture (is also called as Ports & Adapters pattern ) is a architectural pattern used in software design that promotes decoupling from technology and frameworks.
The hexagonal architecture is an attempt to avoid known structural pitfalls in layered architecture.
The idea of Hexagonal Architecture ( Ports & Adapters pattern) is
- create loosely coupled application components that can be easily connected to their software environme
- to makes components exchangeable at any level so that it could be run by different kinds of clients (humans, tests cases, other applications,…)
- it could be tested in isolation from external devices of the real world that the applica
Pitfalls of layered architecture
In layered architecture, over a period of time all the projects get into the problem of excessive coupling across layers.
Alistair Cockburn’a aim was to use architecture to prevent business logic from infrastructure or framework,He accordingly drew application domains as hexagonal shapes.
Imagine a hexagon, here the hexagon is the application itself. Inside the hexagon we just have the things that are important for the business problem that the application is trying to solve.
The hexagon contains the business logic, with no references to any technology, framework or real world device.
In other word, in hexagon, anything inside the hexagon must be free from technology concerns,So the application is technology agnostic.
Outside the hexagon we have any real world thing that the application interacts with.
Overview of what is the Hexagonal Architecture
- Inside the Hexagon with all the business models/logics, outside the Hexagon: the infrastructure
- The dependencies always go from outside toward the inside of the Hexagon, which ensures the isolation of the business domain
- Hexagon depends on nothing but itself,It must not depend on any technical frameworks
Ports And Adaptors in Hexagonal
Adapter is to implement a concrete protocol by which some external system or device can communicate with the application.
An adapter translates between a specific technology and a technology free port.
An adapter is a software component that allows a technology to interact with a port of the hexagon
In most of cases, REST controller are act as a our primary adapter, providing endpoints accessing domain through port (Interface)
A adapter converting a specific technology request into a domain understanding form and pass to the hexagonal through Interface( port)
Ports are the application boundary, in the picture a port is an edge of the hexagon.
To let the outside to interact with the domain, the Hexagon provides business interfaces(Ports) divided in two categories:
API : Those interfaces are implemented by the Hexagon, are also called ‘Inbound interface‘
SPI: (Service Provider Interface) is an interface for a functionality, needed by the hexagon for implementing the business logic. It also called outbound interface
Befits of Hexagonal
- High Maintainability
- Low Technical Debt
- Easily incorporates any channels like sql, ftp, Http, Ftp, JMS, etc.
- Easily tested because it is easy to create a mock when there is a need to implement the contracts.
- Adding new requirements means adding plugins or implementing the contracts.
- Proper separation of concern.
I hope this article will help you understand idea of Hexagonal Architecture and who they are comes together I’ll be glad for any feedback!