Hexagonal Architecture: The Port & Adapter Architecture
Nidhi Agrawal
Posted on June 1, 2022
Architecture is one of the building blocks of any system and choosing it correctly in initial stage will help you in future maintenance and scalability. Most of us have heard about MVC architecture and its vast usage due to ease in development but in the era of evolving technology we need an architecture which can support this dynamic changes.
What is hexagonal architecture and why to adopt it?
In Conventional methodology, we have a Web layer, which receives requests and routes them to a service in the Domain or business layer. The service does some business logic and calls components from the Persistence layer to query for or modify the current state of our domain entities.
You know what? Layers are a solid architecture pattern. If we get them right, we can build domain logic that is independent of the web and persistence layers. We can switch the Web or Persistence technologies without affecting our Domain logic if we feel like it. We can add new features without affecting existing features. With a good, layered architecture, we're keeping our options open and can quickly adapt to changing requirements and external factors.
With Hexagonal architecture you will have enough leverage and hold on your business logic. Its beauty is to put input and outputs at the boundaries of application so in future even if you decide to use another way of getting input or sending back output through new format you won’t need to change your main functionalities.
The centralize idea behind this architecture is business logic should not be dependent on source or format of data it is coming from it can be anything CSV, XML, JSON, Database or REST and it should not be dependent on how we are giving back data through REST or GraphQL API
What is Port and Adapter?
In simple terms port is the way of communication like how your desktops, and laptops have a USB port through which you can connect any device provided they have USB adapters. Similar like that to hexagonal architecture there are ports basically interfaces and their implementation (adapters) which gives you enough control that you can plug in and plug out adapters according to your requirements.
Ports and adapters play a crucial role in abstraction layer creation, this layer keep the core domain of the application isolated from outside tools and technologies being used
This architecture is capable of multiple ports and adapter and hence the name of architecture is hexagonal which gives illusion for multiple port/adaptor combination.
There might be a web adapter that interacts with a web browser, some adapters interacting with external systems, and an adapter that interacts with a database.
The adapters on the left-hand side are adapters that drive our application (because they call our application core) while the adapters on the right-hand side are driven by our application (because they are called by our application core).
To allow communication between the application core and the adapters, the application core provides specific ports. For driving adapters, port is used this port is interface that is implemented by one of the use cases classes in the core and called by the adapter. For a driven adapter, it might be an interface that is implemented by the adapter and called by the core.
Hexagonal architecture falls under layer architecture as we can organize it in layers. The outermost layer consists of adapter which communicate with outers tools/systems, further we the ports and it’s use cases can be combine as a application layer and lastly the domain layer containing entities. The main rule in such an architecture is the dependency rule, which states that all dependencies between those layers must point inward.
Conclusion
The best choice for architecture of your application depends on your requirement of what kind of application you are going to build, its future cases and many more factors. Hexagonal architecture is not the only smart set for application, it involves some level of complexity, When properly implemented and paired with other methodologies, like Domain-Driven Design, Ports and Adapters can ensure an application’s long term stability and extensibility, bringing a great deal of value to the system and the enterprise.
Thanks for reading!
Posted on June 1, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.