r/SpringBoot • • Mar 25 '26

Discussion where to define dto’s in hexagonal architecture?

I’m making an application using hexagonal architecture for the first time and I’m a bit confused on where to put and use my DTO’s. I have three layers: domain, application, infrastructure, where in infrastructure I have my usecases(driving ports) and services(driving adapters). From one side, I need some DTO’s to expect and send data through this service to controllers in infra that call them. From the other side, I need DTO’s for the controllers, that in a regular layered application would also validate received data for example. I also use DDD in my domain, so I have value objects, and since I do, maybe I should rely on validation through those value objects and not some jakarta validation for example?

Hope somebody has some ideas. Thanks in advance

13 Upvotes

8 comments sorted by

9

u/I_Am_Astraeus Mar 25 '26

DTOs live with your web adapters.

You can write mappers to map to and from your domain objects. But the mappers also live with your DTOs/in the web adapter.

You can call it whatever you want. Web, controllers, frontend, UI, adapter you get the gist.

3

u/[deleted] Mar 25 '26

[removed] — view removed comment

4

u/WVAviator Mar 25 '26

All the apps I've worked with do the mapping from entity to dto in the service layer - but I've always felt like it belonged in the controller layer. It always seems like my services end up with two methods - one for providing DTOs to controllers and another for providing entities to other services.

I'll probably start doing it that way myself (mapping in the controller), I just always thought it was supposedly wrong (or at least unconventional) for some reason.

1

u/[deleted] Mar 25 '26

[removed] — view removed comment

1

u/colens26 Mar 25 '26 edited Mar 25 '26

Io uso più o meno il tuo stesso approccio, li creo a livello di applicazione.

Questa è un esempio di come definisco il package di un BC.

https://gist.github.com/giuseppe-trisciuoglio/6b62b66c5761f4c6ee7e1f3c9e855f23