Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
When I encountered the Hexagonal Architecture for the first time I was mind blown.
It was such a simple yet powerful architectural pattern for structuring a system.
Yet I struggled before catching its essence because of the poor choice Alistair Cockburn made to convey his idea.
In my talk Metaphor Driven Design, I explain that when the ligamen between two domains is too threadbare, when the metaphor frame is not correctly chosen, people other than the metaphor author will struggle in getting it.
This is exactly what happened with the Hexagonal Architecture.
There is a core in your system, which is the business logic. And then there are some interfaces around it. And all around again, the external environment.
While this is the key concept of the Hexagonal Architecture, Alistair based his metaphor on premises so unusual that he even felt the need to explain why he chose this name.
The hexagon is not a hexagon because the number six is important, but rather to allow the people doing the drawing to have room to insert ports and adapters as they need, not being constrained by a one-dimensional layered drawing. The term ââhexagonal architectureââ comes from this visual effect.The term âport and adaptersâ picks up the ââpurposesââ of the parts of the drawing. A port identifies a purposeful conversation. There will typically be multiple adapters for any one port, for various technologies that may plug into that port. Typically, these might include a phone answering machine, a human voice, a touch-tone phone, a graphical human interface, a test harness, a batch driver, an http interface, a direct program-to-program interface, a mock (in-memory) database, a real database (perhaps different databases for development, test, and real use).
Itâs unusual for a fresh invented pattern to be published with two alternative names (Hexagonal Architecture, and Port and Adapters). I think this happened exactly because of Alistair being unable to come up with a better, unique name to represent his new idea.
So, why Iâm spending time and words on metaphors?
Because this is one of the most useful architectural pattern ever, still developers struggle to capture its essence because of the cognitive dissonance brought in by those nouns.
And I want to fix it.
The core idea
Your business logic is at the center of your system.
Not spread around many places, but in one, single kernel of code which is agnostic to the rest of the world.
Around it, one or many interfaces. They are mediators between humans or external systems and the business logic.
The interfaces are completely downstream to the business logic.
It means that when something change in the surface of the business logic, all interfaces must adapt. When something changes in the surface of the interfaces, the business logic doesnât care.
This is the essence of the Hexagonal Architecture.
âWhat about the data side?ââââyou may ask.
I think the relation with the data side, as portrayed by Alistair, plays a minor role related to the one with the interfaces.
Choosing your own data representation is way less complicated than deal with external inputs.
This is one of the most useful architectural pattern ever, still developers struggle to capture its essence because of the cognitive dissonance brought in by those nouns.
Why should you use it
Because it focuses on the thing that matters most, namely the business.
How you interact with it is minor. The technology, the protocol is pushed at the boundaries of the system.
APIs, User Interfaces, CSV files, CLI clients. All of them are only means serving a unique purpose.
This is a great starting point if you want your system to behave consistently, because all your knowledge, all your constraints are in one place.
Letâs make an example.
You have a system for keeping record of email addresses.
Human users are supposed to interact with it through a Web Application and external systems via a HTTPÂ API.
Before adding a new email address, you first want to check if it already exists in your system. If it exists do not add it twice.
This is a constraint of your system. This is your business logic. Itâs going to be executed regardless of the interface we use to interact with it.
Hence, you should keep this constraint in a single place.
This is the kind of DRY that I like: knowledge; constraints; business logic.
Letâs say now you want to associate a person to those emails and you have two requirements:
- the personâs name must not be empty;
- the person is required to upload a profile picture;
How do you decide what goes in your business logic and what goes in an interface?
The answer is: look at which level your constraints must be applied.
In case #1, this is a constraint of your business logic. No interface should be allowed to associate a person with an empty name to an existing email address.
And you also donât want to replicate the constraint in every interface. Again, this is what DRY is about.
In case #2, this is a constraint for humans only. Hence, it should go in the Web Application. The business logic will never know about any profile picture.
What if you want to give human users the possibility to upload their profile picture through API?
Totally legitimate. In this case, youâll implement the Hexagonal Architecture pattern again, this time around your Web Application only.
The Hexagonal Architecture pattern is fractal.
One More Advantage
Have you ever heard about CQRS?
Itâs becoming more and more popular since it came out.
Still I see a lot of developers struggling with architectural decisions when it comes to implement it.
The good news with the Hexagonal Architecture is that it naturally brings in CQRS.
Your business logic is write only. Your interfaces own a read model each and proxy commands to the business logic.
Is that simple.
âWhy a read model for each interface?ââââyou may ask.
Because every interface serves a different user or purpose. Hence a different representation of data is required.
The read model is a decision support system.
As such, itâs only useful when itâs specifically designed for the type of user and use case to which an interface is dedicated.
Thatâs why I use the following heuristic: one interface for each type of user of the system.
Itâs easy to build many read models if you publish your business events out into a pub/sub infrastructure.
You can let one listener from each interface pick relevant events and project them into the interface read model.
With the pub/sub messaging pattern, your Hexagonal Architecture is preserved since the business logic is still agnostic about everything around it.
It just publishes messages out. Whoever will pick them is not relevant.
Conclusion
I shed some light on the Hexagonal Architecture and how to tackle it in a more practical way.
I avoided giving the pattern any new name, even if the original one generates a lot of confusion. I instead focused on explaining its essence.
I also showed you how to combine it with CQRS in an extremely effective architectural combination.
There is still a lot to say about this great pattern and I left some questions unanswered:
- how the communication protocol should look like between interfaces and business logic?
- how should you internally structure the business logic?
- how different parts of the business logic communicate with each other?
Iâll show you these and more wonders in some future posts.
For now I can only say: thank you, Alistair. Iâm grateful to you for this amazing knowledge gift.
Ready to become a better developer?
Iâve created a developer toolkit for you. Download it for free and start learning how to write code you will be proud of. If you constantly apply those techniques, youâll become a top software developer.
Get the free developer toolkit here!
If you enjoyed this post, please click the đ button and share to help others find it! Feel free to leave a comment below.You can also subscribe to my Software Development newsletter here: http://eepurl.com/dfQQazRelated books
- Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions
- Clean Architecture: A Craftsmanâs Guide to Software Structure and Design
- Implementing Domain-Driven Design
Demystifying the Hexagon was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.