Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CRUD operations; do you notify whether the insert,update etc. went well?

Tags:

c#

I have a simple question for you (i hope) :)

I have pretty much always used void as a "return" type when doing CRUD operations on 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 considen 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;
}

It actually just comes down to whether you should notify that something was inserted (and went well) or not ?

like image 248
danielovich Avatar asked May 13 '10 13:05

danielovich


People also ask

What are the four basic operations of CRUD?

CRUD is an acronym that comes from the world of computer programming and refers to the four functions that are considered necessary to implement a persistent storage application: create, read, update and delete.

What are the CRUD operations explain with example?

CRUD refers to the four basic operations a software application should be able to perform – Create, Read, Update, and Delete. In such apps, users must be able to create data, have access to the data in the UI by reading the data, update or edit the data, and delete the data.

What helps in implementing CRUD operations?

An application designer has many options for executing CRUD operations. One of the most efficient of choices is to create a set of stored procedures in SQL to execute operations.

Which is used to perform CRUD operations on a table?

CRUD is an acronym for CREATE, READ(SELECT), UPDATE, and DELETE statements in SQL Server. CRUD in database terms can be mentioned as Data Manipulation Language (DML) Statements as well. Data Manipulation Language is used to manage or manipulate the data present inside database Tables.


2 Answers

I typically go with the first option there.

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

Since you have no try/catch block around the Data Access code, the calling code will have to handle that Exception...thus it will know both if and why it failed. If you just returned true/false, the calling code will have no idea why there was a failure (it may or may not care).

like image 145
Justin Niessner Avatar answered Sep 22 '22 21:09

Justin Niessner


I think it would make more sense if in the case where "item == null" that you returned "false". That would indicate that it was a case that you expect to happen not infrequently, and that therefore you don't want it to raise an exception but the calling code could handle the "false" return value.

As it standards, you'll return "true" or there'll be an exception - that doesn't really help you much.

like image 28
philiphobgen Avatar answered Sep 22 '22 21:09

philiphobgen