In this tutorial, we’ll understand the Hexagonal Architecture and go through the key concepts.
1. Hexagonal Architecture
The software development design principle suggests that software that requires very little effort in maintaining its good design. We must consider maintainability as a key design consideration while architecting the software.Hexagonal Architecture is an architectural pattern that helps us develop software that is easy to maintain, manage, scale, and test. It was introduced by Alistair Cockburn in 2006 and this architecture is also known as Ports and Adapters architecture. The idea here is to divide the application into two parts, the inside part and the outside part.
The core application logic belongs to the inside part and the messaging queues, databases, user interfaces, and requests (Soap and REST) belong to the outside part of it. With this design, the core application logic is completely remote from the outside world. To enable the communication between inside and outside parts, we use Ports and Adapters. We will now understand these in the next section.
The ports are nothing but gateways through which communication takes place. It can be an inbound port or an outbound port. The inbound port works as a service interface and exposes the application’s inside part (core) to the outside world. The outbound port, on the other hand, ensures communication from application to the persistence system.
The adapter is an implementation of inbound or outbound ports and they handle the input from the outside world and translate them into the understandable calls. It encapsulates the logic to interact with the outer world system like database and also transforms the communication between core and outside objects. There are two types of adapters.
3.1. Primary Adapters
This adapter implements the inbound adapter. It is also called Driver Adapters, and it drives the application.
3.2. Secondary Adapters
This adapter implements the outbound port. It is also called Driven Adapter, and the application drives it. For example, the connections to database connection, messaging queues connections, and calls to external API calls are examples of secondary adapters. So this architecture suggests exposing multiple endpoints in an application and if we have the right adapter for the port, we can request for a core part of the application. This architecture comprises three layers.
- Domain – The core logic.
- Application – Mediator between Domain and Framework.
- Framework–Implementation of how the external world interacts with the domain
We have understood the Hexagonal Architecture and why it is very essential to design good software applications. It also helps the developer to maintain the application easily and without disturbing the users of this application.