r/SpringBoot • u/vreginalld • 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
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.
1
u/BanaTibor Mar 25 '26
A DTO is needed when you have to move data across boundaries of your app. Typically for serialization/deserialization, REST based communication, read/write database. So these are representations of data which needed in your application. Do not confuse them with domain objects. For example an Employee domain object can be an entity and you should not use it as a DTO, create an EmployeDto which is responsible for representing employee data for communication with outside systems.
1
u/nerd_airfryer Mar 27 '26
Adapters/in/
3
u/[deleted] Mar 25 '26
[removed] — view removed comment
1
u/[deleted] Mar 25 '26
[removed] — view removed comment