Card image

What is Hexagonal Architecture and its practical applications?

system designhexagonal architecturesoftware architectureports and adapters

I. Overview of Software Architecture

Application without architecture (source: Internet)
Application without architecture (source: Internet)
Application with architecture (source: Internet)
Application with architecture (source: Internet)

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?

notion image
  • 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:

notion image

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
    • notion image
  • With business stuff:
    • notion image
  • With delivery stuff:
    • notion image
  • Divide business code into Domain and Use Cases
    • notion image
  • External components may depend on internal ones, but not the other way around
    • notion image
      notion image

VI. Implementation

Logic code is divided into two parts:

notion image
  • Domain
    • models: Create necessary models, types, or interfaces
      • notion image
    • repositories: All types and interfaces related to repositories (a repository is responsible for fetching data from various sources, such as web services, databases, or files)
      • notion image
    • 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)
      • notion image
  • Infrastructufre
    • http: Stores client-related objects (e.g., DTOs received from a repository)
      • notion image
    • instances: Instances of clients and repositories, considered the system’s entry point
      • notion image
        we can move easily from using axios to fetch or ajax by creating new instance for the new one
        we can move easily from using axios to fetch or ajax by creating new instance for the new one
    • repositories: Implements repositories defined in the domain
      • notion image
 

Example provided with VueJs

notion image

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

 

Thank you for reading. Happy coding 🙌