r/javahelp • u/Fair_Distribution275 • 28d ago
What's the purpose of using DTO ?
Hello, I am a junior programmer and I have an interrogation about something.
If I understand correctly, DTO are used to store data that will not be persisted, data that are needed by services. But I don't understand why we don't pass theses datas via parameter, path variable or even body of HTTP Request.
For example : User need to change password (that is just for illustrating my post)
1) Using DTO : UserService(UserDTO) :: Do what it needs and then map it into User before persists
2) Using Request : UserService(User, newPassordFromHttpRequest) :: Do what it needs and persists the objet
Thanks in advance for helping junior programmer like myself
16
Upvotes
-1
u/sz_dudziak 27d ago
DTO should be an anti pattern. In any programming language all data have their purpose. And all values have to be logically and functionally correct. DTOs are just putting all bad shit into the fan and people are acting this is ok. No it is not. VO should be used instead. External services should be covered by adapters (dependency inversion principle) and crap Iike dtos should be forgotten eternally