Hexagonal Architecture for People in a Hurry
In this article, i want do descomplicate in a brief introduction: the concept of Hexagonal Architecture, also known as Ports and Adapters Pattern.
What is it?
The hexagonal architecture is based on three pillars or principles
- Separate: User-side, Business Logic and Server-side;
- Dependencies are going from User-side and Server-side to the Business Logic;
- We connect the application edges by using Ports and Adapters.
Ports
The ports handle all the communications with our core application. They are interfaces that our inner components interact with, without information about who is plugging with them.
Adapters
When an application (connector) has a different plug necessity and the ports can’t handle this modification: We use adapters to convert data from external applications to our core application format.
Example
For this example, imagine a REST API connected with a Frontend Web application.
- Frontend port: The request may have different parameters and may expect a different response formats. We can create an adapter for each frontend sender;
- Adapter: It receives the the request and converts it into a consistent format defined by the port and passes it onto the inner application.
When the data reaches the inner application, they already in the expected format and the application works on them and returns de response in a format that the port expects.
- Port: The port forwards the response to the adapter it received the request from.
- Adapter: The adapter converts the response into a format suitable for the requesting party.
Pros
- Independent solution to external services;
- Separation os business logic;
- Easier to test.
Cons
- More complex architecture than trivial ones (necessity of build the layers);
- Cost of building and maintenance.
Conclusion
“Build your application to work without a UI or database, to which you can run automatic regression tests, deploy when the database is not available, and connect applications without involving them.” (Alistair Cockburn)
It’s one more way to build and study about code and application architecture. The concept of isolation helps to work with microservices and other solutions that follows similar pattern.
I hope this article help you. Thanks!