Hexagonal
Hexagonal Architecture (Ports and Adapters)
Hexagonal Architecture, also known as Ports and Adapters Architecture, is a design pattern that emphasizes separation of concerns and focuses on the interactions between the core business logic (hexagon) and external systems (ports and adapters). It enables flexibility, test-ability, and maintainability by decoupling the application's business logic from external dependencies.
"This approach is an alternative to the traditional layered architecture. " Wikipedia
Overview
Hexagonal Architecture promotes the idea of a self-contained core surrounded by layers of adapters, which handle input/output operations and integrate with external systems. This architectural style facilitates the development of applications that are independent of frameworks, databases, and UI technologies, making them easier to maintain and evolve over time.
Use Cases
Hexagonal Architecture is suitable for a wide range of applications and scenarios, including:
- Micro-services: Hexagonal Architecture provides a clear separation between business logic and infrastructure concerns, making it well-suited for micro-services-based systems.
- Domain-Driven Design (DDD): Hexagonal Architecture aligns closely with DDD principles by focusing on the core domain logic and isolating it from infrastructure and external dependencies.
- Event-Driven Architectures: The decoupled nature of Hexagonal Architecture makes it suitable for event-driven architectures, where components communicate through asynchronous events.
Benefits
- Isolation of Concerns: Hexagonal Architecture separates business logic from external concerns, such as UI frameworks, databases, and third-party services, leading to a more maintainable and testable code base.
- Test-ability: By decoupling the core business logic from external dependencies, Hexagonal Architecture enables easier unit testing and promotes a test-driven development (TDD) approach.
- Flexibility: Hexagonal Architecture allows for easier integration of new components or technologies without impacting the core business logic, providing flexibility to adapt to changing requirements.
- Portability: Applications built using Hexagonal Architecture are often more portable across different environments and platforms, as they are not tightly coupled to specific technologies or frameworks.
Main Components
- Core Business Logic: The core of the application, containing domain entities, use cases, and business rules.
- Ports: Interfaces or contracts defining how the core interacts with the outside world. Ports represent the boundaries of the application and are implemented by adapters.
- Adapters: Implementations of ports that interact with external systems or infrastructure. Adapters translate external inputs into formats that the core can understand and vice versa.
Architecture Components
1. Primary Ports (Input Ports): Define interfaces through which external systems interact with the application's core business logic. Examples include REST APIs, message queues, or command-line interfaces.
2. Secondary Ports (Output Ports): Define interfaces through which the application's core interacts with external systems, such as databases, external APIs, or message brokers.
3. Adapters (Primary and Secondary): Implementations of primary and secondary ports that bridge the gap between the application's core and external systems. Adapters handle input/output operations, data conversion, and error handling.
Hexagonal Architecture Patterns
Ports and Adapters
Ports and Adapters, also known as Hexagonal Architecture, is a design pattern that emphasizes separation of concerns and focuses on the interactions between the core business logic (hexagon) and external systems (ports and adapters). It enables flexibility, test-ability, and maintainability by decoupling the application's business logic from external dependencies.
Dependency Inversion Principle (DIP)
The Dependency Inversion Principle states that high-level modules should not depend on low-level modules but should depend on abstractions. In Hexagonal Architecture, this principle is applied by defining ports as abstractions and having adapters implement these ports, thus inverting the traditional dependency direction.
Separation of Concerns
Hexagonal Architecture promotes separation of concerns by clearly defining boundaries between the core business logic and external systems. This separation allows for easier maintenance, testing, and evolution of the application.
In a micro-services-based system, each micro-service can be designed using Hexagonal Architecture. The core business logic of each micro-service is encapsulated within a hexagon, while adapters handle interactions with external services, databases, and other micro-services.