Hexagonal Architecture - what is it and why is it worth knowing?
Hexagonal architecture, or hexagonal architecture, is a design pattern that is gaining popularity in the programming world. Its goal is to create applications with loosely coupled components that can be easily exchanged and tested independently of external dependencies. In this article, we'll explain what exactly a hexagonal architecture is, what its key elements are, and why you should use it. Finally, we will provide practical examples to help you better understand how to implement this pattern in your daily work.
What is hexagonal architecture?
Hexagonal architecture, also known as ports & adapters architecture, was proposed by Alistair Cockburn in 2005. Its main idea is to separate the business logic (domain) from any external dependencies, such as databases, user interfaces or external APIs. This makes the application more modular, easier to maintain and test.
Unlike traditional layered architecture, where dependencies run from higher layers to lower layers, in hexagonal architecture dependencies are directed from external components to the inside of the application, i.e. the domain. It is the domain that defines the ports - the interfaces of communication, and the adapters are their implementations on the external side.
Key elements of hexagonal architecture
- Domain (Core) This is the heart of the application, where the business logic and use cases reside. The domain does not know the implementation details of external systems and does not depend on them.
- Ports These are the interfaces that define how the application communicates with the outside world. Ports can be input (e.g., accepting requests from the user) or output (e.g., sending data to a database).
- Adapters Implement ports and are responsible for communicating with specific technologies, such as databases, user interfaces, APIs or external systems. Adapters can be easily replaced without affecting business logic.
Advantages of using hexagonal architecture
- Modularity and interchangeability of components Thanks to the clear separation between ports and adapters, technologies can be easily swapped, such as changing the database or user interface without interfering with business logic.
- Ease of testing Business logic is completely decoupled, allowing you to test it in isolation without running external systems.
- Independence on frameworks and technologies Domain code does not depend on specific libraries or frameworks, making it more stable and easier to develop.
- Support for Domain-Driven Design (DDD) Hexagonal architecture works well with the DDD approach because it focuses on the domain and its use cases.
Practical examples of using hexagonal architecture
To get a better understanding of how hexagonal architecture works, it's worth looking at some practical examples from different domains.
E-commerce application
Imagine an online sales application. At the center (domain) we have the logic for managing orders, products and payments. Ports define how the application communicates with the outside world:
- Input port - REST API accepting orders from customers.
- Output port - the interface to the payment system that processes transactions.
Adapters implement these ports:
- REST adapter handles HTTP requests and forwards them to the domain.
- The payment adapter integrates with an external service, such as PayU or Stripe.
With this approach, if we decide to change the payment system in a year's time, we only need to replace the payment adapter without modifying the business logic.
Content management system (CMS)
In a CMS, the domain is responsible for managing articles, users and permissions. Ports can include:
- Input port - a user interface (such as an admin panel) or API for publishing content.
- Output port - access to a database or file system for storing images.
Adapters are specific implementations:
- Web frontend as an input adapter.
- A database adapter, such as for PostgreSQL or MongoDB.
- A cloud adapter, if storing files on Amazon S3.
Thanks to the hexagonal architecture, we can easily replace the database or add new publishing channels without affecting the domain.
How to start implementing hexagonal architecture?
- Define the domain and its boundaries Focus on the business logic and use cases that are the heart of your application.
- Define the ports Define interfaces for communication with the outside world - both input and output.
- Create adapters Implement ports for specific technologies, such as databases, APIs, user interface.
- Test the domain independently By isolating business logic, you can write unit tests without running external components.
Summary
Hexagonal architecture is a pattern that helps create flexible, testable and maintainable applications. By clearly separating the domain from external dependencies, applications based on hexagonal architecture are more resistant to technological changes and easier to develop.
Practical examples show how this pattern can be applied to different types of applications - from e-commerce to CMS to mobile applications. If you want your projects to be more modular and easier to maintain, you should consider implementing hexagonal architecture.
Do you have questions about implementing hexagonal architecture or want to learn about tools that will make working with this pattern easier? Contact us!