Return value of save/update/delete

Posts   
 
    
orenpeled
User
Posts: 53
Joined: 25-Jul-2005
# Posted on: 23-Mar-2006 09:08:40   

Hi everybody!

The following methods return Boolean: (1) SaveEntity (2) DeleteEntity

The following methods return Int32: (3) SaveEntityCollection (4) UpdateEntitiesDirectly (5) DeleteEntityCollection (6) DeleteEntitisDirectly

I understand that (4, 5, 6) should return an indication on the number of updated/deleted records. However, I can't seem to find a situation in which I pass a collection of entities to save in (3), and it returns a number smaller than the number of the passed entities due to a failure. I would expect an exception to be thrown in this case.

Same thing about (1, 2) - why should I get False in case of a failure? Wouldn't an exception be more reasonable when failing to save/delete a specific entity?

In other words - when would I want to accept a situation in which insertion/deletion fails (the return value False) and move on?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 23-Mar-2006 10:12:35   

orenpeled wrote:

Hi everybody!

The following methods return Boolean: (1) SaveEntity (2) DeleteEntity

The following methods return Int32: (3) SaveEntityCollection (4) UpdateEntitiesDirectly (5) DeleteEntityCollection (6) DeleteEntitisDirectly

I understand that (4, 5, 6) should return an indication on the number of updated/deleted records. However, I can't seem to find a situation in which I pass a collection of entities to save in (3), and it returns a number smaller than the number of the passed entities due to a failure. I would expect an exception to be thrown in this case.

You will get an exception, it just returns the # of entities saved. So if you have a collection and 1 entity isn't changed, you'll get a number lower than the # of entities in the collection.

Same thing about (1, 2) - why should I get False in case of a failure? Wouldn't an exception be more reasonable when failing to save/delete a specific entity?

Only in recursive situations. When the library was originally designed, exceptions were considered to be exceptional, they still are, and therefore if a save fails, an exception would probably cause havoc, while 'false' would signal that the save failed. You will get an exception though in some situations, like when an error occurs inside the db. You won't get an exception when a refetch fails for example, you then will get false. Recursive saves always throw an exception as that's needed to quit the transaction.

It's a thing we didn't change from the beginning to avoid people having applications breaking when an exception was thrown all of a sudden while they didn't expect it.

Frans Bouma | Lead developer LLBLGen Pro
coderdude
User
Posts: 10
Joined: 24-Apr-2006
# Posted on: 25-Apr-2006 21:23:02   

While I understand the reasoning behind this for backward-compatability sake, I'm a little worried about starting a new project like that. I normally write exceptions that occur when performing CRUD operations to a log file, however, it wouldn't be much help at all to simply log "FAILED" without any exception information.

Personally, knowing why the operation failed is a MUST for debugging.

What is everyone else doing for this? Is there a common method or work-around that can be implemented to get this kind of feedback?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 26-Apr-2006 09:55:15   

If the save operation fails due to a database error (FK violation for example, or NULL inserted into a non-nullable field), you'll get an exception. You'll only not get an exception when you save an entity which isn't dirty and/or there's nothing to save. Every DB error is resulting into an exception.

Frans Bouma | Lead developer LLBLGen Pro
coderdude
User
Posts: 10
Joined: 24-Apr-2006
# Posted on: 26-Apr-2006 19:45:40   

Ok. That makes me feel better. Thanks.