fbpx
Hero Illustration
0 Comments
Design Pattern, Hexagonal Architecture, Mitrais, Software Development

Hexagonal Architecture Design Pattern

What is Hexagonal Architecture?

Alistair Cockburn developed hexagonal architecture, or ports and adapters architecture, and used it for designing software applications. We insert inputs and outputs at the edge of our design with hexagonal architecture. It allows us to isolate the central logic of the application. Since our inputs and outputs are on edge, we can switch their handlers without affecting our core code.

The sides of the hexagonal architecture represent ports in the model. Rather than using a circle to show specific information regarding the ports, Cockburn chose this flat-sided shape. Below is an example interpretation of a hexagonal architecture diagram:

Clean Architecture Diagram — Hexagonal Model – Dor Moshe’s Blog, <https://dormoshe.io/daily-news/hexagonal-model-architecture-in-go-language-2037.993>

The model is balanced with many external services on the left and others on the right. The hexagonal architecture was developed as a model for structuring many application parts and created to handle I/O. I/O is on the model’s external area. The grey area is where you will see the adapters, the hexagon’s sides are the ports, and the middle is where the application and domain are found. The business/application/domain logic lives in the core, and there are no specifications.

Benefits Over Traditional Layered Architecture

The hexagonal architecture was a departure from the traditional layered architecture. One of the significant differences with hexagonal architecture is that the user interface can be replaced. Using hexagonal architecture instead of layered architecture can be helpful due to its many benefits, such as:

Maintainability: Our applications are highly regarded and trusted because of changes in one area’s components.
Flexibility: Quickly toggle between different applications; without changing source code, you can produce new adapters.
Simple testing: Easily test in isolation as the code is separated from the implementation information of the outside.
Agnostic: You can develop the inner core before building external services as the application is independent of exterior areas.

However, the hexagonal architecture does have a few flaws. A few of them are:

Decoupling: Intermediate classes could affect the overall performance of our application.
Debugging: Adapters can often be challenging to understand and debug.
Complex: Hexagonal architecture can be confusing as it can be challenging to decide what elements should go on the outside.

Hexagonal Architecture Layers

Creating dependency rules can decouple the layers. The hexagonal architecture is also called the Ports and Adapters. The outer layers may only depend on the inner layers, and the inner layers should not rely on the outer ones. Each layer is defined as follows.

Infrastructure Layer

The Infrastructure Layer contains code interfaces with application infrastructure – controllers, UI, persistence, and gateways to external systems. A web framework or persistence library will provide many of the objects in this layer. Concretions of domain repositories are placed in this layer, while the virtual interfaces are defined in the domain layer.

Application Layer

The Application Layer provides an API for all functionality provided by the application. It accepts commands from the client (web, API, or CLI) and translates them into values understood by the domain layer. For example, a RegisterUser service would accept a Data Transfer Object containing a new user’s credentials and delegate responsibility for creating a user to the domain layer.

Domain Layer

The Domain Layer contains any core domain logic. It deals entirely with domain concepts and lacks knowledge of the outer layers.

Use Cases

Some use cases for hexagonal architecture include:

  • A banking application that allows us to send money from one account to another
  • A system that will enable us to apply for a loan, go through verification, and receive an update when our application updates
  • A loyalty application that allows us to register customers and upgrade or downgrade their memberships

Conclusion

Not all applications can benefit from the Hexagonal or Ports, and Adapters Architecture. Hexagonal is complicated and challenging. However, it can ensure an application’s long-term stability and extensibility if the architecture is implemented correctly and coupled with other methodologies, like Domain-Driven Design, Ports, and Adapters. It can also bring significant value to the system and the enterprise.

References

Alistair Cockburn’s original paper on Hexagonal Architecture
https://hexaviewtech.com/domain-driven-design-and-the-hexagonal-architecture/
https://medium.com/ssense-tech/hexagonal-architecture-there-are-always-two-sides-to-every-story-bc0780ed7d9c
Excellent read on Domain-Driven Design: Everything You Always Wanted to Know About it, But Were Afraid to Ask ?
Eric Evans’ Domain-Driven Design: Tackling Complexity in the Heart of Software
Bob Martin’s OO Design Quality Metrics
Bob Martin’s Agile Software Development Principles, Patterns and Practices

Author: Alfath Tariq, Software Engineer Program Analyst

Contact us to learn more!

Please complete the brief information below and we will follow up shortly.

    ** All fields are required
    Leave a comment