What is Hexagonal Architecture?
Hexagonal architecture, also known as Ports and Adapters or the Onion architecture, is a software design pattern that has gained popularity due to its ability to improve the maintainability, testability, and adaptability of an application. The layers of the application are represented as a hexagon, with the innermost layer being the core of the application and the outer layers representing various external interfaces.
Components of the Hexagonal Architecture
Domain Model
At the centre of the architecture is the Domain Model, which represents the application’s core business logic and entities. It is responsible for implementing the main functionality and rules that the application is built around. One of the critical principles of hexagonal architecture is that the core of the application should not depend on any specific external interface.
Ports
Ports are the interfaces that define the communication points between the application’s core logic and the external world. They can be categorized as:
- Primary or Driving Ports - define the operations that the application exposes to external actors. They represent the use cases or application services that external systems or users initiate.
- Secondary or Driven Ports - define the operations that the application needs from external services or components. They represent the dependencies or resources required for the application to function.
Adapters
Adapters are the components that translate the interactions between the external world and the core business logic by implementing the ports. There are two types:
- Primary or Driving Adapters - convert external inputs into a format that the core logic can understand.
- Secondary or Driven Adapters - implement secondary ports by providing the required functionality to interact with external services or resources.