Skip to Content
Tech PrinciplesArchitecturePatternsHexagonal Architecture

Hexagonal Architecture ⬡

Hexagonal architecture (also known as ports and adapters architecture) is a software design pattern that separates the core domain logic of an application from external factors like infrastructure, user interfaces, and peripherals.

Overview

The hexagonal architecture divides the application into several layers:

  • Domain - Contains business logic and core entities
  • Application - Contains use case services that rely on the domain
  • Adapters - Contains adapters that convert data between external ports and internal models

Hexagonal Architecture

The domain and application layers are at the core, surrounded by adapters that communicate with the core via ports and interfaces.

Key Principles

  • The domain is isolated from outside concerns
  • Ports are the interface between adapters (edges) and the application (containing the domain)

Benefits

  • Loose coupling between domain and external resources
  • Easier to test and change domain logic independently
  • Adapters make it easy to plug in new infrastructures, or the other way Domain can be reused for different adapters

Project structure

        • SendMoneyController.java (Entrypoint)
        • AccountMapper.java
        • AccountPersistenceAdapter.java (impl. LoadAccountPort)
        • ActivityRepository.java
        • AccountRepository.java
        • SendMoneyUseCase.java (Interface)
        • LoadAccountPort.java (Interface)
        • Money.java
        • Account.java
        • SendMoneyService.java (impl. SendMoneyUseCase)

Conclusion

Overall, hexagonal architecture provides flexibility by isolating the core domain and making it easier to swap out external dependencies.

Source

Last updated on