As per the software development design principle, the software which requires the minimum effort of maintenance is considered as good design. That is, maintenance should be the key point which an architect must consider. In this article, one such architecture, known as Hexagonal Architecture which makes the software easy to maintain, manage, test, and scale is discussed.
Hexagonal architecture is a term coined by Alistair Cockburn in 2006. The other name of Hexagonal architecture is Ports And Adapters architecture. This architecture divides an application into two parts namely, the inside part and the outside part. The core logic of an application is considered as the inside part. The database, UI, and messaging queues could be the outside part. In doing so, the core application logic has been isolated completely from the outside world. Now the communication between these two parts can happen through Port and Adapters. Now, let’s understand what each of these means.
- The Ports: The Ports acts as a gateway through which communication takes place as an inbound or outbound port. An Inbound port is something like a service interface that exposes the core logic to the outside world. An outbound port is something like a repository interface that facilitates communication from application to persistence system.
- The adapters: The adapters act as an implementation of a port that handles user input and translate it into the language-specific call. It basically encapsulates the logic to interact with outer systems such as message queues, databases, etc. It also transforms the communication between external objects and core. The adaptors are again of two types.
- Primary Adapters: It drives the application using the inbound port of an application and also called as Driving adapters. Examples of primary adapters could be WebViews or Rest Controllers.
- Secondary Adapters: This is an implementation of an outbound port that is driven by the application and also called as Driven adaptors. Connection with messaging queues, databases, and external API calls are some of the examples of Secondary adapters.
Therefore, the hexagonal architecture talks about exposing multiple endpoints in an application for communication purposes. If we have the right adapter for our port, our request will get entertained. This architecture is a layered architecture and mainly consists of three layers, Framework, Application, and Domain.
- Domain: It is a core business logic layer and the implementation details of the outer layers are hidden with this.
- Application: It acts as a mediator between the Domain layer and the Framework layer.
- Framework: This layer has all the implementation details that how a domain layer will interact with the external world.
Illustrative Example: Let’s understand this architecture with a real-time example. We will be designing a Cake Service application using Spring Boot. You can create a normal Spring or Maven-based project as well, depending on your convenience. The following are the different parts in the example:
- Domain: Core of the application. Create a Cake class with its attributes, to keep it simple we will just add name here.
- Inbound port: Define an interface through which our core application will enable its communication. It exposes the core application to the outside world.
- Outbound port: Create one more interface to create or access the outside world i.e., Cake.
- Primary Adapters: A controller could be our primary adapter which will provide endpoints for creating and fetching the resources.
We can create one more interface for CakeRestUI as follows:
- Secondary Adapters: This will be the implementation of an outbound port. Since CakeRepository is our outbound port, so let’s implement it.
- Communication between the core to the Data Source: Finally, let’s create an implementation class that will be responsible for communication between core application to the data source using an outbound port.
We have finally implemented all the required methods in the given example. The following is the output on running the above code:
Now, lets create some Cake for the above example using the REST API. The following API is used to push the cakes into the repository. Since we are creating and adding the data, we use the POST request. For example:
- API: [POST]: http://localhost:8080/cake
Input Body{ "name" : "Black Forest" }
- API: [POST]: http://localhost:8080/cake
Input Body{ "name" : "Red Velvet" }
- API: [GET]: http://localhost:8080/cake
Output[ { "name": "Black Forest" }, { "name": "Red Velvet" } ]
Advantages of the Hexagonal architecture:
- Easy to maintain: Since the core application logic(classes and objects) is isolated from the outside world and it is loosely coupled, it is easier to maintain. It is easier to add some new features in either of the layers without touching the other one.
- Easy to adapt new changes: Since all the layers are independent and if we want to add or replace a new database, we just need to replace or add the database adapters, without changing the domain logic of an application.
- Easy to test: Testing becomes easy. We can write the test cases for each layer by just mocking the ports using the mock adapters.
Source : https://www.geeksforgeeks.org/hexagonal-architecture-in-java/
No comments:
Post a Comment