ā How to structure DTOs
If you're making a DTO, do you make it like this
Or like this
Or like this
Basically, should there be one DTO that is used for all OperationTypes, or one endpoint and DTO per type (Update/Delete/Etc)?
And the last one is quite bad, I'm sure, but if the Student is complex and long, would it ever be acceptable?
15 Replies
None of those, I never use public fields
But assuming those were properties, each DTO should only ever hold exactly as much data as it needs
And, ideally, each should be used only once
So you don't reuse the same DTO for editing users and returning blogpost author
idk why adding 'public' is just reflex but the {get; set;} I thought I'd skip for brevity š
So then followup question....
If I have some process that specifically update's a student's name, and another one that updates their address. Would you use two separate endpoints, two separate DTOs?
Yep
(honestly at that scale I think you'd skip the DTO and just take in UpdateStudentName(int id, string name), though?)
If you can bind it properly, sure, that works
Personally, I just create a simple record next to the handler method and use that
That seems like a lot of duplicated effort, though, two separate endpoints to update two separate values
Like they're both going to start by retrieving the student via ID. Which isn't a lot of duplicated code, but it's not none
Well, to be perfectly honest, I use MediatR in the first place, so that issue solves itself lol
Use just a single method, then
What do you mean?
One update endpoint
But you said you'd have two DTOs š what you have there, makes sense to me
I mean, you can do either
You said initially you have two handlers, one for name, one for address
it's just awkward trying to find that line because both ways are annoying in different ways
You'd have two DTOs in this case
If you have one update endpoint, you'd have one DTO
either way, better than the way we do it now, which is basically
š¤¢
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.