C
C#4d ago
Cydo

✅ When and when not to use a DTO?

I know you shouldn't really send the exact database model to the client and instead choose only the data that should be sent to the client, but when if you are querying the database for a super specific projection of data, and the DTO would end up looking exactly like that projection. Should you still make the DTO or is it perfectly fine to just return the projection as the response, or query the database directly into the DTO.?
2 Replies
Jimmacle
Jimmacle4d ago
generally the projection is the DTO the main goal is to avoid accidentally changing your API surface when you change deeper parts of your application
Cydo
CydoOP4d ago
Perfect, so then this is fine. This is just a simple get request, that pulls data back i want to display all neatly on cards i show on my client, and nothing deeper in the app is affected 😄 So in my case where im using CQRS i can directly make the Response which is my DTO just be returned directly.

Did you find this page helpful?