✅ Implementing delete repository method & controller | Web Api
I would love to know how to properly implement delete repository method, in particularly how to handle possible null values.
16 Replies
$details
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, what you expect the result to be, what .NET version you are using and what platform/environment (if any) are relevant to your question. Upload code here https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code! (see $code for more information on how to paste your code)
In the simplest case, trying to delete something that doesnt exist isn't actually an error, as the end result is the same: the item doesn't exist.
So if you are trying to write idempotent code, if the query doesnt delete anything, thats fine.
So this code is just about fine?
Or better,
Or better yet, don't use repositories
Absolutely not.
save changes when nothing was done is silly. doing
DbSet.Remove(null)
is silly.
if you know its null, just stop there.
that said, ZZZZZZZZs code is much nicer
and more performant
its a single query, instead of twoI like it
Not that the result value of
SaveChangesAsync
also indicates if something was deleted at all in this case
I assume the same happends with ExecuteDeleteAsync
Ye
It returns rows modified, so anything above 0 means there were changes
It returns the amount of rows affected
Same for
.ExecuteUpdateAsync()
Yeah, affected is a better word
Could you explain please meaning of '_ ='
A discard
Could as well be
But using a discard explicitly states "I know this method returns something, but I don't care"
Thanks
A matter of preference, really
How explicit you want to be