Hexagonal Architecture

Hexagonal architecture is a popular architectural pattern in software development. It is an architectural style that moves a programmer’s focus from conceptual layers to a distinction between the software’s inside and outside parts.

GraphQL has a role beyond API Query Language- being the backbone of application Integration
background Coditation

Hexagonal Architecture

Hexagonal architecture, also known as ports and adapters architecture, is a software design pattern that has gained popularity in recent years due to its ability to improve the maintainability, testability, and adaptability of an application. This architecture is called "hexagonal" because the layers of the application are represented as a hexagon, with the innermost layer being the core of the application and the outer layers representing various external interfaces.

The main advantage of hexagonal architecture is that it promotes the separation of concerns and the independent development of each layer. This means that changes to one layer do not necessarily require changes to other layers, making it easier to maintain and evolve an application over time. Additionally, the hexagonal architecture makes it easier to test individual components in isolation, which can improve the overall reliability and stability of the application.

In hexagonal architecture, the core of the application is represented by the hexagon's center. This layer contains the business logic and the domain model, which define the core functionality of the application. The hexagon's outer layers represent various external interfaces, such as the user interface, web services, and databases. These external interfaces are connected to the core through a set of well-defined ports, which provide a consistent and predictable way for the core to interact with the external interfaces.

Ports and adapters are the mechanisms that connect the core of the application to its external interfaces. A port is a well-defined interface that specifies how the core of the application can interact with an external interface. An adapter is an implementation of an external interface that connects to the core through a port.

Ports and adapters are an important part of hexagonal architecture because they enable the core of the application to be used with different external interfaces without being tied to any specific implementation. This makes the application more flexible and adaptable to changing requirements.

For example, suppose an application has a core layer that contains the business logic and a user interface layer that presents the data to the user. In this case, the core layer would define a port that specifies how it can receive user input and how it can provide data to the user interface. The user interface layer would implement an adapter that connects to the core through the defined port. This would enable the core of the application to be used with any user interface that implements the adapter, without being tied to a specific user interface implementation.

Adapters are used to connect the core of the application to external systems, such as databases, APIs, or user interfaces. There are two types of adapters: inbound and outbound.

  • Inbound adapters receive requests from external systems and convert them into a format that the core of the application can understand. For example, an inbound adapter might receive an HTTP request and convert it into a method call on a domain service.
  • Outbound adapters are the opposite. They receive requests from the core of the application and convert them into a format that can be understood by external systems. For example, an outbound adapter might receive a method call from a domain service and convert it into an HTTP response.

Together, inbound and outbound adapters form the "ports" of the hexagonal architecture, allowing the core of the application to communicate with the outside world.

Adapters are the implementations of external interfaces that connect to the core of the application through well-defined ports. Several types of adapters can be used in hexagonal architecture, depending on the specific requirements of the application.

Some common types of adapters in hexagonal architecture include:

  • User interface adapters: These adapters implement the user interface of the application, and connect to the core through a port that specifies how the core can receive user input and provide data to the user interface.
  • Web service adapters: These adapters implement web services that the application can use to communicate with other systems or services, and connect to the core through a port that specifies how the core can send and receive data through the web service.
  • Database adapters: These adapters implement the database access layer of the application, and connect to the core through a port that specifies how the core can retrieve and store data in the database.
  • File system adapters: These adapters implement the file system access layer of the application, and connect to the core through a port that specifies how the core can read and write files in the file system.
  • External service adapters: These adapters implement access to external services, such as payment gateways or email services, and connect to the core through a port that specifies how the core can interact with the external service.

Overall, the specific types of adapters used in hexagonal architecture will depend on the requirements of the application and the external interfaces that it needs to interact with. The important thing is that the adapters connect to the core through well-defined ports, which enable the core of the application to be used with any compatible external interface.

One of the critical principles of hexagonal architecture is that the core of the application should not depend on any specific external interface. Instead, the external interfaces should depend on the core and be implemented as adapters that connect to the core through the defined ports. This means that the application's core can be used with any compatible external interface, making it more flexible and adaptable to changing requirements.

There are several benefits of using hexagonal architecture. Let's take a look:

  1. It enables independent development & evolution of each layer, which makes it easier to maintain & evolve an application over time.
  2. It is easier to test individual components in isolation, which can improve the overall reliability & stability of the application.
  3. It allows the use of different external interfaces with the same core, making the application more flexible & adaptable to changing requirements.

In the above example, the UserService is the core of the application, and it depends on the DomainRepository interface to store and retrieve data. This allows the UserService to be tested and used without needing to know the details of how the data is stored and retrieved. The DomainRepository could be implemented using a database, a remote API, or any other data storage mechanism, and the UserService would not need to change.

In conclusion, hexagonal architecture is a valuable software design pattern that can help to improve the maintainability, testability, and adaptability of an application. By separating the core of the application from its external interfaces, the hexagonal architecture enables independent development and evolution of each layer and makes it easier to test and ensure the reliability of the application.

I am Saurabh Mahajan. a tech aficionado and passionate software engineer who loves making bad jokes. Always on the lookout for the best coffee in town and the next big thing in software. A weekend gamer and an absolute nerd.

Want to receive update about our upcoming podcast?

Thanks for joining our newsletter.
Oops! Something went wrong.