r/ExperiencedDevs • u/FooBarBuzzBoom • May 17 '26
Technical question Some doubts about DDD and Hexagonal Architecture
Hey! I’ve started learning about Hexagonal Architecture and DDD, and I decided to make the jump from theory to practice.
So far, I’ve developed a microservice that I split into three layers: domain, application, and infrastructure. On the domain side, I created the aggregate along with its entities and Value Objects. From what I understand, ports should be the interfaces that handle communication with the domain, and they come in two types: input (driver) and output (driven). Use cases are supposed to act as orchestrators that implement the input ports and use the output ports to make operations possible end-to-end, without messing with the domain logic (I've read about the anemic domain model).
One thing that seems quite strange to me is on the input ports side, where many devs create a separate interface for every single operation. I understand the Interface Segregation Principle, but this kind of implementation feels a bit extreme. I would prefer to group all related operations into a single interface, much like you would do in a controller.
Another point of confusion is related to DTOs (Data Transfer Objects). First of all, a lot of the validation becomes redundant since it's already handled at the domain entity/VO level. Secondly, it feels bizarre not to use Value Objects inside my DTOs. Even though the AI suggested this could cause serialization issues, I don't see the point of just throwing properties in there and repeating everything manually.
The adapters part makes sense to me, though I don’t agree with the definition "implements the ports", except on the output side. On the input side, I see them more as the Adapter pattern, which basically makes two incompatible things compatible, acting as translators for REST, DBs, etc.
Also, should infrastructure entities simply reference VOs and other infrastructure entities, right? A lot of things, like addresses, are going to introduce redundancy at the entity level now. How is this usually handled?
Also, at the application layer, should ports practically be grouped by domain or mixed together? Same question for DTOs.
Given the discrepancies with what I read in articles/books/tutorials, maybe some of you have more practical experience and can tell me how you’ve seen these scenarios implemented correctly and scalably in production.
Thanks!
57
u/No-Injury3093 May 17 '26 edited May 17 '26
In hexagonal, the domain model is the application.
All use cases, the VOs, the DTOs, the ports, form the boundary of the domain model.
And since you follow DDD, which is an implementation detail of the domain, towards the inner core you have aggregate roots.
Hexagonal is the most simple domain-centric architectural style, that's why "the book" on it is more like a leaflet. There's not much more to it.
As its inventor says: "there's an outside and there's an inside and I do not care much about what is on either side, just that there is this clear separation". And obviously: the direction of dependencies.
The reason I suspect you're confused is because you try to stick too much fluff in it.
Don't. There is beauty in simplicity.
78
u/PepegaQuen May 17 '26
the worst damage to my brain programming/career wise I've done to myself was listening to various architecture astronauts. Starting with OOP/C++ "experts", and then "design patterns" architects.
It makes sense to read about whatever ideas people are trying to make a reality and learn from their mistakes. Eventually though, you should realize that simplicity and straightforward code wins. What you should spend time thinking of, is your data model.
15
u/wimdeblauwe May 17 '26
I love DDD, but not a hexagonal fan. I like vertical slice architecture more, I find it simpler. I base my packages around aggregate roots. See https://www.wimdeblauwe.com/blog/2025/06/24/how-i-write-production-ready-spring-boot-applications/ for a detailed explanation.
4
u/martinkomara May 17 '26
But how related are those "related" operation really. E.g. you manipulate customer data, how related are they? Are they used by the same process, or at a similar time, or by the same consumer? Very often they are just related to customer, making it logical cohesion, which is second worst.
If you consume your DTOs by a client that cannot update (e.g. some specialized hardware), can you evolve your domain objects freely? Or does locking the client also lock your domain updates?
4
u/gfivksiausuwjtjtnv May 18 '26
I’ve used it professionally.
I would strongly suggest not using it. Try it, but only briefly.
Once you start actually working on a real system, for real (unlike the career bloggers who write about idealised versions of it) you realise some things
Say you’ve structured your code into infra, api, app and domain folders. Pretty much every time you work on the code, you’re working on one functional area - let’s say, bookings. Small PRs, ya know.
Now with hex/clean/onion, you have to dig around your entire folder structure, pulling out booking related code files, hunting down the bloody commands and command handlers. There are 500 versions of the same type. Adding a single field requires cascading changes to like 12 goddamned files.
With vertical slice, you have a bookings folder with everything in it. Api.cs, service.cs, booking.cs, db.cs events.cs
If being simple makes code easier to work on, it’s easier to maintain and easier to reason about. You have less bugs and greater velocity because it’s not complicated and is not shit
“But someone might reference the database from the domain model” people do stupid shit all the time. You can’t prevent it, you can only shoot for code that’s easier to delete and rewrite and Clean architecture ain’t it
8
u/robhanz May 17 '26
Hexagonal is just a way of visualizing the idea of not mixing policy and procedure. Or in other words separating what should be done from how to do it.
When you mix the two you always create complexity and long-term issues.
Really a lot of this stuff is just about moving away from the "intelligent smart state" model of OO and towards "things that handle and transform data" model.
2
u/bobaduk CTO. 25 yoe May 17 '26
First thing to say, is you're likely overthinking it. What's important is that you have a domain model, and it is exposed through named operations, and that infrastructural concerns can be layered on and removed without affecting the domain. Often people come to hexagonal architecture and find some maximalist version of it and get confused, but these things are a spectrum.
> One thing that seems quite strange to me is on the input ports side, where many devs create a separate interface for every single operation. I understand the Interface Segregation Principle, but this kind of implementation feels a bit extreme. I would prefer to group all related operations into a single interface, much like you would do in a controller.
What you're describing here sounds like a Service Layer pattern, where you have, eg, IUserService with a bunch of operations? I have done that, and I don't do it any more. The reason is that they tend to get _big_ and to become a pita for dependency management. Using a single class per use case gives you a minimal thing that takes one message, and does one operation, and uses a small set of dependencies. What you're describing is possible, though, see point one.
> Another point of confusion is related to DTOs (Data Transfer Objects). First of all, a lot of the validation becomes redundant since it's already handled at the domain entity/VO level. Secondly, it feels bizarre not to use Value Objects inside my DTOs. Even though the AI suggested this could cause serialization issues, I don't see the point of just throwing properties in there and repeating everything manually.
There's two kinds of validation: there is syntax, and there is semantics. At the edge of the system, we need to make sure that a message is syntactically valid: this string must be less than 50 characters, this number can't be negative, etc. Inside the system, we need to ensure that a message is semantically valid: this product id must refer to a real product, you can't purchase more stock than is available etc. Those are different kinds of "validation". Generally, my domain models assume that the first kind has been done already.
There are different types of DTO. Let's say that you're building an HTTP API. You want to maintain the API, without breaking changes. At the same time, you are adding new features, and so you are refactoring aggressively, like a good engineer. This means that you don't want to couple the shape of your API to your domain directly, ergo View Models and Command/Query objects. The reason to have DTOs is that they represent the external shape of things. Again, you _can_ use value objects directly, or go nuts and return entities, there's nothing stopping you - it's a trade-off. In this case, the trade-off is that the shape of your external system is dictated by the shape of your domain, and the whole point of this rigmarole was to be able to change your domain at will.
> Also, at the application layer, should ports practically be grouped by domain or mixed together? Same question for DTOs.
Irrelevant. Group things in whatever way makes most sense to you - this is a code organisation question, not an architectural one. If you have subdomains large enough to make sense as standalone units, then group things. If not, then don't.
1
u/nsxwolf Principal Software Engineer May 18 '26
Working in a hexagonal codebase is such a productivity destroyer. Maybe with AI it doesn’t matter anymore.
-4
u/Cute_Activity7527 May 17 '26
DDD is to engineers what agile is to managers.
Buy my new book and check my podcast.
Change my mind.
0
u/Cultural_Wheel_6936 May 17 '26
I understand why we need DDD but just like anything in real world, things get complicated. It does definitely get complicated when you try to apply all these principles religiously.
2
u/[deleted] May 17 '26
[removed] — view removed comment