Background Shape
Hero Shape
Written by Armakuni
Sep 11, 2024

Hexagonal Architecture: Easy Integration Using Ports and Adapters

Table of Content

Hexagonal Architecture: Easy Integration Using Ports and Adapters

In software engineering, there are multiple ways to design a system, each with its own unique characteristics and design principles. Different design approaches often aim for similar goals though. Among the most discussed patterns in object-oriented architecture is hexagon architecture. 

The hexagonal architecture (aka the ports and adapters architecture) is a way to structure your applications. This architecture is drawn as one hexagon inside another. The code inside the inner hexagon is your pure business logic (domain model) and the code in the outer hexagon is your infrastructure integration code (known as adapters). Any communication between the inner domain mode code and the real world must pass through the outer layer (the adapters).

The sides of the hexagon represent different faces/aspects/use cases of the application. The two sides on the left often show user interaction with the system, and the two on the right often show backend activity. The number of sides is not important, the system may have many more use cases but the diagram just demonstrates that it’s more than one on each side.

The key part of the hexagonal architecture is the inner hexagon (domain model) has no knowledge of the implementations of the adapters (outer hexagon). That is, all dependency arrow point inwards.

Ports are the parts of the domain model (inner hexagon) that allow interaction with the outer hexagon. Adapters are modules in the outer hexagon that convert the interactions of the outside world (out side the hexagons) into the interactions with the ports.

There a two types of port — primary and secondary.

Primary ports trigger behaviour as a result of some event happening outside the system. These are implemented as public commands on the domain which can be invoked by an adapter; a typical adapter here might be an MVC controller.

Secondary ports are invoked from inside the domain when something needs to be triggered in the outside world (e.g. an entity is persisted or a message is sent). These are generally interfaces, with the adapter being a concrete implementation (the adapter pattern — #17).

https://alistair.cockburn.us/hexagonal-architecture/

Comparing hexagonal architecture with onion and layered architectures

Hexagonal architecture keeps the core business logic separate from external systems using ports and adapters, while onion architecture organises the application in layers, with the core business logic at the centre.

Hexagonal architecture makes it easier to change external systems without affecting the core, whereas onion architecture focuses on protecting the core business logic from outside changes.

Compared to layered architecture, hexagonal architecture is more flexible. Layered architecture has a strict top-to-bottom structure with presentation, business logic, and data access layers.

Hexagonal architecture’s ability to let the core work independently of external services makes it more adaptable to new technologies and easier to modify without causing widespread changes. Layered architecture often leads to tightly connected layers, where changes in one layer might require changes in others.

How Armakuni can help

Each architecture has its strengths. The choice between these architectures depends on project needs, such as 

  • How important it is to keep core logic separate from external systems, 
  • How central the business rules are, and 
  • How easily the project needs to adapt to changing technologies or requirements.

At Armakuni, we help businesses use modern architectural patterns like hexagonal architecture to build systems that are scalable and adaptable. Our hands-on coaching ensures your teams have the skills to implement these patterns effectively, boosting agility and speeding up the delivery of high-quality products.

We work closely with your teams to promote best practices and drive positive change. By embedding our experts within your teams, we make the transition to a more resilient and flexible architecture smooth, empowering your organisation to quickly respond to changing market demands.

Got Questions?

Reach out, and we’ll have answers for you pronto.

Send message
Success Icon
Request Received

Thank you for reaching out to Armakuni. We have received your message and will get back to you as soon as possible.

Oops! Something went wrong while submitting the form.

Got Questions?

Reach out, and we’ll have answers for you pronto.

Send message

Meet the speakers

No items found.