We are working with hexagonal architecture in our new application. So many layers for everything while we know fairly sure we will have only one implementation for almost everything.
Of course we have unit test implementations, in this case it’s regarding production code. Nobody is going to use hexagonal architecture just for unit tests.
Only one implementation from the start? What of unit tests?
You can replace specific dependencies in your test runner instead of pretending you'll ever need interfaces for more than one implementation in your production code.
Besides, not all kinds of units can be declared to implement an interface. For example, a typescript module can be a cohesive unit of behavior, but unlike a class it cannot be specified to implement any predefined interface.
It's also known as ports and adapters architecture. Abstract your side effects away from your core logic via virtual classes, then inject actual implementation depending on runtime environment.
it's a more opinionated version of coding to interfaces. Basically, "code to interfaces, but be smart about where you have interfaces"
Don't arbitrarily shove virtual classes between every real class everywhere in your code. Instead, put those interface classes around things that are irrelevant to your business rules. This way you build and test those rules isolated from any real environment.
For example, I might have a service where I need to publish to a message queue. If you write your code to call publish directly, the only way to test this is to inspect the actual message queue for a published message. If you put the publish call behind an abstract base class you can build your package and let something downstream inject a real communications transport for deployment or a mocked one for testing.
My best work experience was using hexagonal architecture. Really enjoyed it. It took a while to reaaaaally understand how everything was suposed to work, but after some time everything was so smooth I think for that particular company it was worth it
And never forget to have an Interface in Front of every Service class. Someone from the other end of the universe might one day need another implementation for this!
I attempted a Spring based protocol agnostic file transfer tool (with corporate audits) for work one time. It did get subclassed for ftps, but I was the one who had to do it. Still, it was easy, so worth doing.
Have a mirror, because that person is likely going to be you.
Today, the fashionable thing to is to use dependency injection for everything over inheritance. Ask any old C++ programmer the joys of dealing with some brittle class inheritance mess.
I find inheritance is almost always the wrong solution. By inheritance I mean actually sharing behavior, not just a pure virtual class in C++ or something.
However, there are some use cases. Mainly for what are effectively structs.
Hey though occasionally your design ends up being the entire backbone for a bunch of implementations. At work a while back when I was primarily writing API adapters I was tired of rewriting code all the time and wrote a base classes for ReST API consumption and OAuth mixins. Now whenever my company needs to write a custom adapter that uses OAuth and/or consumes a ReST API they inherit from either/both of these classes and override as necessary, often just one or two methods. There are dozens of child implementations in production using my paradigm.
Now, I have to admit this is one of the only times this has happened, and I do often overcomplicate things with too much OOP and no one ever extends it. But sometimes!
83
u/Groentekroket Sep 14 '26
We are working with hexagonal architecture in our new application. So many layers for everything while we know fairly sure we will have only one implementation for almost everything.
99
u/BreakfastAmbitious29 Sep 14 '26
I'm in this photo and I don't like it
46
u/beatlz-too Sep 14 '26
I swear this is the first non-AI related meme I've seen here for like a year at least
20
u/nintendo_shill Sep 14 '26
Don't forget to use Kubernetes for you enterprise app with 6 daily users (2 are from QA)
85
u/thoughts_n_calcs Sep 14 '26
And never forget to have an Interface in Front of every Service class.
Someone from the other end of the universe might one day need another implementation for this!
13
u/rinnakan Sep 14 '26
Two decades ago, I worked on a self-written "cms" or "portal" that required 40 steps to add an empty page.
It supported multi-channel, but ofc there was only one
11
u/uvero Sep 14 '26
YAGNI? My motto is IDCIAGNI
5
u/Djelimon Sep 14 '26
I attempted a Spring based protocol agnostic file transfer tool (with corporate audits) for work one time. It did get subclassed for ftps, but I was the one who had to do it. Still, it was easy, so worth doing.
6
u/Lemortheureux Sep 14 '26
How dare you. A project is in the backlog, any day now...
3
u/Outrageous-Text-4117 Sep 14 '26
waiting for people to thank me for the micro service made
3
u/DerpWyvern Sep 14 '26
and the moment you decide "fk abstraction", you get whole system rewrites
5
u/xaervagon Sep 14 '26
Have a mirror, because that person is likely going to be you.
Today, the fashionable thing to is to use dependency injection for everything over inheritance. Ask any old C++ programmer the joys of dealing with some brittle class inheritance mess.
2
u/disposepriority Sep 14 '26
I simply reject MRs with single implementation interfaces and/or base classes unless the work requiring a second implementation is already known.
2
u/remy_porter Sep 14 '26
If they subclass it they’re ruining my design! Inject it as a dependency!
2
u/slaymaker1907 Sep 14 '26
I find inheritance is almost always the wrong solution. By inheritance I mean actually sharing behavior, not just a pure virtual class in C++ or something.
However, there are some use cases. Mainly for what are effectively structs.
1
u/TorbenKoehn Sep 14 '26
Inheritance used as an extension mechanism is a lost case in any way or order
1
u/anxious_and_stupid Sep 14 '26
I design and encapsulates the whole timer class with modern c++ meta template and stuff... just use it only twice in my project
1
u/socialis-philosophus Sep 15 '26
Sounds like someone that doesn't write unit tests. But that none of my business... (sips tea)
0
u/diegotbn Sep 14 '26
Hey though occasionally your design ends up being the entire backbone for a bunch of implementations. At work a while back when I was primarily writing API adapters I was tired of rewriting code all the time and wrote a base classes for ReST API consumption and OAuth mixins. Now whenever my company needs to write a custom adapter that uses OAuth and/or consumes a ReST API they inherit from either/both of these classes and override as necessary, often just one or two methods. There are dozens of child implementations in production using my paradigm.
Now, I have to admit this is one of the only times this has happened, and I do often overcomplicate things with too much OOP and no one ever extends it. But sometimes!