CRUD operations; Are you reporting whether they are inserted, updated, etc. well?

I have a simple question for you (hopefully) :)

I almost always use void as the type of "return" when performing CRUD operations with data.

Eg. Consider this code:

public void Insert(IAuctionItem item) { if (item == null) { AuctionLogger.LogException(new ArgumentNullException("item is null")); } _dataStore.DataContext.AuctionItems.InsertOnSubmit((AuctionItem)item); _dataStore.DataContext.SubmitChanges(); } 

and then parse this code:

 public bool Insert(IAuctionItem item) { if (item == null) { AuctionLogger.LogException(new ArgumentNullException("item is null")); } _dataStore.DataContext.AuctionItems.InsertOnSubmit((AuctionItem)item); _dataStore.DataContext.SubmitChanges(); return true; } 

In fact, it all comes down to the fact that you should notify that something has been inserted (and went well) or not?

+7
design c #
source share
6 answers

I usually use the first option.

Given your code, if something goes wrong with the insert, an exception will be thrown.

Since you do not have a try / catch block around the data access code, the calling code will have to handle this exception ... this way it will know how and why it failed. If you just returned true / false, the calling code will not know why the failure occurred (maybe it may not be).

+15
source share

I think it would be more reasonable if you returned false if "item == null". This indicates that you expect this to happen infrequently, and therefore you do not want it to throw an exception, but the calling code could handle the return value of "false".

As in the standards, you will return the "truth" or an exception will occur - this does not help you much.

+1
source share

Do not fight with the frame in which you find yourself. If you write C code where return values ​​are the most common mechanism for passing errors (due to the lack of a better built-in construct), use this.

The .NET base class libraries use Exceptions for messaging, and their absence means that everything is in order. Since almost all code uses BCL, most of it will be written to wait for exceptions, unless it gets into a library written as if C # was C without exception support, each call should be wrapped in if (! MyObject .DoSomething) {System.Writeline ("Damn");} block.

For the next developer to use your code (which may be after several years when you forgot how you did it before), it will hurt to start writing all the call code to take advantage of the error conditions passed as return values, like changing values ​​in the output parameter as user events, as callbacks, as messages in a queue, or any other possible way to report an error or lack thereof.

+1
source share

I think it depends. Displays that your user wants to add a new message to the forum. And the addition is not for some reason, then if you do not inform the user, they will never know that something is wrong. The best way is to make another exception with a good message for them

And if this does not apply to the user, and you are already logged in the database log, you do not have to worry about returning or not.

0
source share

I think it is a good idea to notify the user if the operation went well or not. No matter how much you test your code and try to come up with out of the box, most likely, during its existence, the software will encounter a problem that you cannot cope with, which leads to incorrect operation. Using notifications, in my opinion, allows the user to take action, a kind of plan B, if you like the program to fail. This action can be either a simple job or informing people from the IT department so that they can fix it. I would rather click on this extra OK button than find out that something went wrong when it was too late.

0
source share

You should stick with void if you need more data - use variables for this, since you will need specific data (and this can be several lines / lines), and the excpetion mechanism is a good solution to handle the error.

so .. if you want to know how many lines were affected if sp returned something ect ... - the return type will limit you.

0
source share

All Articles