javaMicroservices

Introduction to Hexagonal Architecture

Hexagonal Architecture or ports and adapters architecture is an architectural pattern used in software design.

It aims at creating loosely coupled application components that can be easily connected to their software environment by means of ports and adapters. This makes components exchangeable at any level and facilitates test automation.

The idea of Hexagonal Architecture is to put inputs and outputs at the edges of our design. Business logic should not depend on whether we expose a REST or a GraphQL API, and it should not depend on where we get data from — a database, a microservice API exposed via gRPC, or REST.

hexagonal
Hexagonal Architecture

In hexagonal architecture, the application has three parts:

  • The User-Side
  • The Business Logic
  • Backing Service

The User-Side:

This is the part through which the user or any external application will interact with the application. In other words, the user interface of the application.

Business Logic:

Leveraged from the Hexagonal Architecture, the three main concepts that define our business logic are Entities, Repositories, and Interactors.

  • Entities are the domain objects (e.g., a Movie or a Shooting Location) — they have no knowledge of where they’re stored (unlike Active Record in Ruby on Rails or the Java Persistence API).
  • Repositories are the interfaces to getting entities as well as creating and changing them. They keep a list of methods that are used to communicate with data sources and return a single entity or a list of entities. (e.g. UserRepository).
  • Interactors are classes that orchestrate and perform domain actions — think of Service Objects or Use Case Objects. They implement complex business rules and validation logic specific to a domain action (e.g., onboarding a production).

Backing Services:

These are services which support the business logic. They each serve a specific purpose and provide data/services to the application. They interact with the business logic layer and are replaceable as long as the communication contract between the two layers is maintained.

A few examples:

  • Data sources
  • Cache aside servers like Redis
  • Notification services
  • Another service like a payment gateway
  • In microservices context, another microservice.
hexa

Intent and principles

The intent is to make the core of our application immune to changes in the communication with other layers. Those concerns are to be handled at the boundary of our hexagon.

Ports

Ports are what our core application interacts with. Ports stay consistent for the inner application no matter what happens outside them. They are interfaces that the inner components interact with without knowing whats being plugged into them.

Adapters

Ports are staying consistent but we still want to be able to plug multiple applications into them when needed. These applications could have different needs and may not comply with the interface defined by the ports. This is where our adapters come in. Their purpose is to convert the data provided by the outer applications into a format digestible for the inner application.

Sample Code: https://github.com/kolaparthisrini/buckpal

Refer: https://skolaparthi.com/category/microservices/

Summary:

Hexagonal Architecture promotes the separation of concerns by encapsulating logic in different layers of the application. This enables a higher level of isolation, testability and control over your business specific code.

Loading

Translate »