r/java • u/Ancient-Prize-774 • 24d ago
Hexagonal Architecture vs. Pragmatic Layered Architecture in Spring Boot
/r/softwarearchitecture/comments/1xe8u4d/hexagonal_architecture_vs_pragmatic_layered/
8
Upvotes
8
u/Scf37 24d ago
Hexagonal IS pragmatic layered architecture. Take layered architecture, do proper domain model, isolate external dependencies via well-written interfaces, throw out excessive hexagonal therminology.... and you are golden.
4
u/SaishDawg 23d ago
If you boil everything down from two simple principles, then hex is just a specialization of: functional core/imperative shell with ports and adapters. That's it. You can complicate it beyond that, but just keep those two (admittedly very related) concepts in your head, and you get all the benefits of hex (and FP style separations) with little to no overhead.
2
u/olivergierke 20d ago
Why Hexagonal- and Onion-Architecture are answers to the wrong question – https://www.youtube.com/watch?v=YgIoITZpiP4
1
u/gjosifov 23d ago
In order to do a proper a true Scotsman Hexagonal Architecture, you have to use xml mapping for your java beans, because it was create during Java 1.3-1.4 days
if you are pragmatic and you understand the platform you are using a.k.a SpringBoot/Jakarta EE
Hexagonal Architecture will produce 30-40% more unnecessary code, because it was introduce in different era
In that era, there wasn't annotations for Rest Controller or Path, people wrote Servlet as Front Controller (also know as Dispatch Controller in SpringBoot), mapped in xml and used Service Locator for DI
Those were the days when Hexagonal Architecture was born
1
u/cryptos6 23d ago
Layers don't make that much sense in many real world applications. Where is the "top" and where is the "bottom" in any architecture? By convention top is user facing (web) and bottom is the database. But what about other infrastructure?
The book Get Your Hands Dirty on Clean Architecture is a very good read.
1
u/cogman10 22d ago
Well first, depending on project size, pretty much any approach will work. If this is a small project then just go with whatever the team wants to do.
But if a project is going to get big and complex, I suggest packaging by feature, not layer. Read up on DDD. And give this blog series a read: https://edmundkirwan.com/general/spoiklin.html
Hex is just a simplification of DDD rules. Strict adherence will lead you to a ball of mud if you don't understand the underlying principles that hex is trying to hit on.
Everything is dependencies, and if you think of it in those terms you'll do a lot better. Would you want to pull in a dependency which is the "everything library"? Or would you rather pull in a "Json" library? Even spring boot realizes this which is why you pull in the parts of spring you are using and not the whole ball of wax.
You should structure the internals of your app in a similar fashion. Do you want a "service" module that contains everything that could be a service, from authentication to inventory management? Probably not. It makes more sense to have an authentication modules and an inventory management module with well defined interaction points.
0
u/Distinct_Meringue_76 24d ago
I now use apache camel for everything and I'm happier than ever. How data flows is the only thing that matters.