02_02_Hexagonal_Architecture
02_02_Hexagonal_Architecture
Adapters)
Key Components
• Ports: Interfaces that represent the primary ways of interaction (usually
Application and Domain layer interfaces).
o Primary ports correspond to the use cases the application should fulfill.
o Secondary ports represent the operations the application needs from
external concerns like databases and third-party services.
• Adapters: Actual implementations that adapt a port to a specific technology or
use-case.
o Inbound adapters adapt incoming requests to a port (e.g., REST API,
GraphQL, CLI).
o Outbound adapters adapt port operations to external systems (e.g.,
Database, Message Queue).
• Core Application: The business logic and domain model sit at the center of the
hexagon. It remains isolated from the outside world, which interacts with it
through ports and adapters.
Advantages
• Decoupling: Easy to change one part without affecting others.
• Testability: Makes it easier to write unit tests as you can easily mock the ports.
• Flexibility: Can plug in different adapters without changing the business logic.
• Maintainability: Clean separation of concerns leads to easier maintenance.
1
Disadvantages
• Complexity: Can be overkill for simple applications.
• Learning Curve: Takes time to understand and implement correctly.
• Potential for Overengineering: Easy to get carried away, creating unnecessary
ports and adapters.
Clean Architecture
• Proposed by Robert C. Martin, aims for separation of concerns among rules,
database, and UI.
• A broader architectural pattern, encompasses more than just the structure of the
application, includes considerations for deployment, UI, etc.