Hexagonal Architecture

Software Engineering Topic of the Day #35

Credit: Originally written and published by Tom Oram on Armakuni’s internal slack

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/

Q1: Is this different to the onion architecture or a layered architecture?

Previous
Previous

The SOLID Principles

Next
Next

PaaS vs FaaS: Which one should I choose to run my microservices on?