
What is Hexagonal Architecture and its practical applications?
system designhexagonal architecturesoftware architectureports and adapters
I. Overview of Software Architecture


II. Why Do We Need to Architect Applications?
- To create applications that are easy to scale and maintain
- To avoid being affected by frameworks or libraries
- To make sharing easier among team members and stakeholders
- To reduce deployment time between teams (FrontEnd/Backend/MobileApp)
III. What Is Hexagonal Architecture?

- Hexagonal Architecture (also known as the ports and adapters architecture) is a software design pattern. It focuses on building applications around business/application logic that is not affected by or dependent on any external components, interacting with them only through ports/adapters.
- Because of its independence, you can easily switch between data sources (libraries/frameworks) without impacting the business/application logic. Inputs and outputs of data sources are placed on the sides of the hexagon.
- You can implement business logic before choosing a library or framework.
- Fun fact 😃: Why a hexagon (six sides) and not another shape? The hexagon is just a name; in reality, it can be any polygon, with as many ports/adapters as needed to connect to external data sources.
IV. Why Was Hexagonal Architecture Created?
The article below highlights common issues in codebases:
.png?table=block&id=2133cb4a-6caf-8039-bd8e-f91f72e16e8b&cache=v2)
As we can see:
- Code files (e.g., functions/*.php) containing mixed JavaScript, jQuery, HTML, and business logic all in one file
- Logic separated only by blank lines
- Very large files with both business and UI logic mixed together
How can we review or maintain it. In my case, no way (just kidding, I still have to fix it in real life LOL 😂)
V. Solution Provided by Hexagonal Architecture
- Separate business/application logic from other components

- With business stuff:

- With delivery stuff:

- Divide business code into Domain and Use Cases

- External components may depend on internal ones, but not the other way around


VI. Implementation
Logic code is divided into two parts:

- Domain
models
: Create necessary models, types, or interfacesrepositories
: All types and interfaces related to repositories (a repository is responsible for fetching data from various sources, such as web services, databases, or files)services
: Responsible for interacting with models and executing related actions (e.g., a method to check if a product can be added to the cart if stock > 0)



- Infrastructufre
http
: Stores client-related objects (e.g., DTOs received from a repository)instances
: Instances of clients and repositories, considered the system’s entry pointrepositories
: Implements repositories defined in the domain




Example provided with VueJs

VII. Summary
Advantages:
✅ Organizes code around business rules, not frameworks or libraries
✅ Easy to control dependencies, only allowing inner layers to be depended on
✅ Supports testing and maintenance well
✅ Codebase is easy to extend
✅ Application is independent of technology evolution (libraries/frameworks)
✅ Reduces time spent choosing technology for the project
✅ Codebase can be shared across frontend, backend, and mobile apps
Disadvantages:
❌ More complex code organization, takes more time
VIII. References
- Other online resources
Thank you for reading. Happy coding 🙌