tags:

views:

54

answers:

1

It may be a subjective question but I want to know what people feel is the best pattern for behavior of an object's save or delete methods?

When you setup a class that will contain Db record information I usualy end up declaring the objects Delete() and Save() methods as void. As these methods usualy take direct action on other properties of the object do we need to return anything else to confirm the action? On fails I just let the error handleing framework or implementig code handle any exception fall out.

I have seen alot of objects that send back a rows affected int or other return, what is the 'expert' opinion on this?

i.e. this:

public void Delete()
{
   if (this.objectID == null || this.objectID == -1) { throw new exNotDbObject() }
   //Do dB operations
   this.objectID = null;
   this.TimeRetrievedFromDb = null;

}

vs this:

public int Delete()
{
   if (this.objectID == null || this.objectID == -1) { throw new exNotDbObject() }
   int retVal = dataLayer.DeleteObj(this.objectID);
   return retVal;
}
+1  A: 

I like the int Delete() myself. I can then tell how many (if any) records were deleted, and unless there is some problem, the program can continue as expected.

I suppose you could do void Delete() and throw an exception if no records (or multiple ones for that matter) were deleted, but I never liked this approach as that always assumed that I know what the expected outcome of the deletion was.

Kevin
Well Kevin at 20k your def an expert so thanks for the weigh in, I ended up going with a rowcount return, more useful in collections.
Tj Kellie