What is Hexagonal Architecture?

Ece Demirel
3 min readMay 16, 2024

--

Introduction

This architecture is known as the Ports and Adapters Architecture. It provides the creation of loosely coupled, interchangeable application components. The components can easily connect to the software environments. This makes components exchangeable.

➡️ WHY? This method helped in achieving the goal of separating the business (domain) layer from the database, user interface, framework, and other external components.

➡️ WHERE? Hexagonal structure works effectively for large applications that have multiple inputs and outputs.

https://www.arhohuttunen.com/hexagonal-architecture/
https://www.arhohuttunen.com/hexagonal-architecture/

In Hexagonal Architecture, inputs by users and external systems can reach the application at a port via an adapter.

Ports and Adapters

Ports

The term “ports” simply means entry points to the application core. Communication with the outside is provided with ports. There are two types of ports; incoming which can be requests, events from external systems, etc., and outgoing which can be sent commands, queries to external systems, etc. These communications happen with adapters.

  • The application core can expose its functionality to the outside world through an input port, also known as a driving port.
  • Another kind of interface that the application core uses to access resources outside of itself (such as obtaining data from a database) is an output port, also known as a driven port.

Adapters

An adapter is a port implementation that connects the main program logic to a specific external dependency, such as a message system, web framework, or database driver. Technical factors like caching, security, logging, and error handling are also handled through it.

  • Primary (driver) adapters start a flow by accepting signals, events, and requests from a particular technology and converting them to a port.
  • Secondary (driven) adapters serve as either an output or support for the program, converting commands through a port to a certain technology.
https://scalastic.io/en/hexagonal-architecture/#2-principles-of-hexagonal-architecture
https://scalastic.io/en/hexagonal-architecture/#2-principles-of-hexagonal-architecture

Features

  • Isolation of Logic: The business logic or core app is separated from other parts like external systems. It provides loose coupling. It ensures that a decoupling from external systems and their technical details.
  • Independence: The business part is independent. Dependencies are established from the exterior to the interior of the hexagon.
  • Better Domain Modeling: Facilitated by a clearer separation of concerns and a focus on business logic.
  • High cohesion: Ensured by handling technical concerns with adapters, keeping the focus on business and application logic within the core.
  • Improved Testability: Enhanced ability to test due to clear separation of concerns.
  • Easier Maintenance: Simplified maintenance efforts by isolating core logic from technical dependencies.
  • Scalability: Improved scalability through a modular and adaptable architecture.
  • Exchangeability: Ability to change adapters without modifying the core, promoting system flexibility.
  • Reusability: Different adapters can be used, enhancing component reusability.
  • Improved Readability: Clearer code organization and separation of concerns leading to improved code readability.

Drawbacks

  • It is more complex because of multiple components.
  • It may require more code so it can be difficult to understand and debug.
  • For developers who are not familiar with the pattern, the learning curve for hexagonal architecture could be more challenging.

References

--

--