Tj Kellie
Tj Kellie

Reputation: 6476

What type of method to use for an object Delete or Save?

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;
}

Upvotes: 3

Views: 130

Answers (1)

kemiller2002
kemiller2002

Reputation: 115488

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.

Upvotes: 1

Related Questions