In traditional 3-tier architecture, we have the presentation layer, business logic layer, and data layer. These layers are often tightly coupled, which makes changes to one layer (e.g., switching databases or user interfaces) impact the others. To reduce this coupling, developers typically use techniques like interfaces and dependency injection.
However, Hexagonal Architecture takes this further by inherently ensuring loose coupling and a cleaner separation of concerns. Here’s how it works:
- At the center of Hexagonal Architecture lies the business logic, which contains the core rules and processes of the application.
- Instead of directly interacting with external systems, the architecture introduces Ports and Adapters to facilitate communication:
- Ports: These act as interfaces that define how the application interacts with the outside world. They handle the read and write operations, enabling communication between the business logic and external systems.
- Adapters: These are like converters that transform incoming requests from the ports into a format that the application can process. Adapters also handle outgoing data, converting it to match the expectations of external systems (e.g., databases, file systems, or APIs).
In simpler terms:
- Ports define what can be done (the contract).
- Adapters implement how it is done (the implementation).
This separation allows the business logic to remain independent of external systems, making it easy to swap or modify external components (e.g., replacing a database or adding a new API endpoint) without affecting the core logic.
reference:
- Jorge Barbosa : Designing an application in a microservice architecture
- Alex Hyett:
- Previous Post