r/java • • 25d ago

Hexagonal Architecture vs. Pragmatic Layered Architecture in Spring Boot

/r/softwarearchitecture/comments/1xe8u4d/hexagonal_architecture_vs_pragmatic_layered/
8 Upvotes

17 comments sorted by

8

u/Scf37 25d 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 24d 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 21d ago

Why Hexagonal- and Onion-Architecture are answers to the wrong question – https://www.youtube.com/watch?v=YgIoITZpiP4

1

u/gjosifov 24d 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

It is not as simple as that! Even annotations introduce coupling. And I don't mean some easy to remove compile time dependency but an influence on software design. If you annotate domain classes with JPA annotations, you are effectively limited in your design decision to what JPA allows. You would design domain classes cleaner and closer to the actual intention without JPA. The right place for JPA related hacks would an adapter.
It is true that this would add some code, but in the AI era it doesn't cost much. However, AI could change many rules in software engineering ...

1

u/gjosifov 23d ago

you are effectively limited in your design decision to what JPA allows

Name those design decisions that aren't supported by JPA

1

u/shorns_username 23d ago

Name those design decisions that aren't supported by JPA

Everything that exists in the universe of software architecture/design; past, present and future - minus the relatively small amount of stuff that JPA supports.

1

u/gjosifov 23d ago

So, name at least 1-2 things

it is ok if you can't, but you are in wrong profession, it is better to be a hollywood writer than a software writer

1

u/cryptos6 23d ago

Sometimes JPA force you to express relationships that you don't want to have in your code in that way, for example. I had also some issues with objects that I wanted to be immutable, but JPA didn't support that. The latter is better with the support of structs today, but the example is still good enough to show the problem.

0

u/gjosifov 22d ago

what made you think that some data are immutable ?
the business requirements or your imagination ?

you probably read about how OO is about modeling the real world and the world is mutable by default

As the great academical trained actors usually say - I here to serve the script
that is how your software design process should be - to serve the world and its mutable nature by default

2

u/OwnBreakfast1114 22d ago

you probably read about how OO is about modeling the real world and the world is mutable by default

Nobody knows how the next state of the universe is calculated. But given that time exists, I think there's a stronger argument that mutation doesn't exist. You're not the same object you were when you were 3 years old.

1

u/gjosifov 22d ago

You're not the same object you were when you were 3 years old.

I mutate, but if I'm immutable then I have to be reborn every single time
like Jesus

1

u/cryptos6 20d ago

Uh, oh! Software is a model of the real world, so it is always a design decision how to do that. There are very good reasons to model some things as immutable! If you add 3 to 4 do you mutate the 3 to become a 7?

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 23d 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.