Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what should CRUD (Insert and Update) functions return ?

I could return nothing .. I could return true/false to show if there are any problems.. I could return an int being the primary key of what was inserted or updated.. I could create a custom object to return more information.. perhaps hold a collection of all the validation errors or something that may have occurred..

So it is not something I am stuck with but I just seem to randomly pick different way so would like to hear a best practice for these common functions..

I am using C#/Entities/Repository pattern.

like image 930
punkouter Avatar asked Apr 05 '13 15:04

punkouter


1 Answers

It's really a matter of personal preference. You might want to distinguish between commands and queries.

So creates, updates and deletes would be commands and I would expect them to return void.

Reads would be queries and would return IQueryable.

However, if you routinely want to use an inserted object directly after inserting it you may want to return the object as a result of the insert.

like image 131
Andy Nichols Avatar answered Sep 29 '22 18:09

Andy Nichols