[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.programming

ORM: when to raise an exception: on object creation or on use?

Victor Porton

9/4/2015 2:16:00 PM

Should an ORM implementation query the DB as soon as an active record is
created in order to check that the queried row exists in DB, or is it OK to
throw an exception later when attempting to access a field (for an
nonexistent row)?

--
Victor Porton - http://porton...
5 Answers

Dmitry A. Kazakov

9/4/2015 3:51:00 PM

0

On Fri, 04 Sep 2015 17:15:53 +0300, Victor Porton wrote:

> Should an ORM implementation query the DB as soon as an active record is
> created in order to check that the queried row exists in DB, or is it OK to
> throw an exception later when attempting to access a field (for an
> nonexistent row)?

The rule is simple, only valid objects shall exist. If an object is to
reference only an existing row, then you cannot create one that does not do
that.

Another SW design rule is that an error has to be detected as early as
possible. If an invalid reference is an error then...

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-...

Victor Porton

9/4/2015 4:22:00 PM

0

Dmitry A. Kazakov wrote:

> On Fri, 04 Sep 2015 17:15:53 +0300, Victor Porton wrote:
>
>> Should an ORM implementation query the DB as soon as an active record is
>> created in order to check that the queried row exists in DB, or is it OK
>> to throw an exception later when attempting to access a field (for an
>> nonexistent row)?
>
> The rule is simple, only valid objects shall exist. If an object is to
> reference only an existing row, then you cannot create one that does not
> do that.
>
> Another SW design rule is that an error has to be detected as early as
> possible. If an invalid reference is an error then...

Got your answer.

I am going to follow your advice, taking into account however that this way
an otherwise unnecessary query to the DB is done, what makes the code less
efficient in terms of speed and server load.

Dmitry, do you confirm that the exception should be thrown as soon as
possible, even if this requires querying of otherwise unnecessary DB fields?

--
Victor Porton - http://porton...

Dmitry A. Kazakov

9/4/2015 4:54:00 PM

0

On Fri, 04 Sep 2015 19:22:23 +0300, Victor Porton wrote:

> Dmitry A. Kazakov wrote:
>
>> On Fri, 04 Sep 2015 17:15:53 +0300, Victor Porton wrote:
>>
>>> Should an ORM implementation query the DB as soon as an active record is
>>> created in order to check that the queried row exists in DB, or is it OK
>>> to throw an exception later when attempting to access a field (for an
>>> nonexistent row)?
>>
>> The rule is simple, only valid objects shall exist. If an object is to
>> reference only an existing row, then you cannot create one that does not
>> do that.
>>
>> Another SW design rule is that an error has to be detected as early as
>> possible. If an invalid reference is an error then...
>
> Got your answer.
>
> I am going to follow your advice, taking into account however that this way
> an otherwise unnecessary query to the DB is done, what makes the code less
> efficient in terms of speed and server load.
>
> Dmitry, do you confirm that the exception should be thrown as soon as
> possible, even if this requires querying of otherwise unnecessary DB fields?

No, you need not check for consistency of the DB, unless it must be
consistent before you do anything. There is an infinite number of things
one could check but never does. The rule of early detection applies only to
the program logic.

There is an old saying, never test for bugs you are not going to fix. It
sounds cynical but it is true. If your program can do its job without
checking then checking is wasting time and, moreover, a bug, since this may
prevent your program from working.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-...

Victor Porton

9/4/2015 6:17:00 PM

0

Dmitry A. Kazakov wrote:

> On Fri, 04 Sep 2015 19:22:23 +0300, Victor Porton wrote:
>
>> Dmitry A. Kazakov wrote:
>>
>>> On Fri, 04 Sep 2015 17:15:53 +0300, Victor Porton wrote:
>>>
>>>> Should an ORM implementation query the DB as soon as an active record
>>>> is created in order to check that the queried row exists in DB, or is
>>>> it OK to throw an exception later when attempting to access a field
>>>> (for an nonexistent row)?
>>>
>>> The rule is simple, only valid objects shall exist. If an object is to
>>> reference only an existing row, then you cannot create one that does not
>>> do that.
>>>
>>> Another SW design rule is that an error has to be detected as early as
>>> possible. If an invalid reference is an error then...
>>
>> Got your answer.
>>
>> I am going to follow your advice, taking into account however that this
>> way an otherwise unnecessary query to the DB is done, what makes the code
>> less efficient in terms of speed and server load.
>>
>> Dmitry, do you confirm that the exception should be thrown as soon as
>> possible, even if this requires querying of otherwise unnecessary DB
>> fields?
>
> No, you need not check for consistency of the DB, unless it must be
> consistent before you do anything. There is an infinite number of things
> one could check but never does. The rule of early detection applies only
> to the program logic.
>
> There is an old saying, never test for bugs you are not going to fix. It
> sounds cynical but it is true. If your program can do its job without
> checking then checking is wasting time and, moreover, a bug, since this
> may prevent your program from working.

It looks like for me that you contradict your self: You first answered my
question "no" then "yes".

What is your final advice for me? (no (there is need to check row existence
on object creation) or yes (there is no need to check row existence on
object creation)

Don't ask for a particular situation. It is yet not quite known, I am just
writing some abstract foundational classes for our company, which may be
used in different situations.

--
Victor Porton - http://porton...

Dmitry A. Kazakov

9/4/2015 6:39:00 PM

0

On Fri, 04 Sep 2015 21:17:01 +0300, Victor Porton wrote:

> Dmitry A. Kazakov wrote:
>
>> On Fri, 04 Sep 2015 19:22:23 +0300, Victor Porton wrote:
>>
>>> Dmitry A. Kazakov wrote:
>>>
>>>> On Fri, 04 Sep 2015 17:15:53 +0300, Victor Porton wrote:
>>>>
>>>>> Should an ORM implementation query the DB as soon as an active record
>>>>> is created in order to check that the queried row exists in DB, or is
>>>>> it OK to throw an exception later when attempting to access a field
>>>>> (for an nonexistent row)?
>>>>
>>>> The rule is simple, only valid objects shall exist. If an object is to
>>>> reference only an existing row, then you cannot create one that does not
>>>> do that.
>>>>
>>>> Another SW design rule is that an error has to be detected as early as
>>>> possible. If an invalid reference is an error then...
>>>
>>> Got your answer.
>>>
>>> I am going to follow your advice, taking into account however that this
>>> way an otherwise unnecessary query to the DB is done, what makes the code
>>> less efficient in terms of speed and server load.
>>>
>>> Dmitry, do you confirm that the exception should be thrown as soon as
>>> possible, even if this requires querying of otherwise unnecessary DB
>>> fields?
>>
>> No, you need not check for consistency of the DB, unless it must be
>> consistent before you do anything. There is an infinite number of things
>> one could check but never does. The rule of early detection applies only
>> to the program logic.
>>
>> There is an old saying, never test for bugs you are not going to fix. It
>> sounds cynical but it is true. If your program can do its job without
>> checking then checking is wasting time and, moreover, a bug, since this
>> may prevent your program from working.
>
> It looks like for me that you contradict your self: You first answered my
> question "no" then "yes".

There is no contradiction. Yes applies to the program functionality. If an
object that refers to a row is non-functional when the row does not exist.
Thus you must check that.

> What is your final advice for me? (no (there is need to check row existence
> on object creation) or yes (there is no need to check row existence on
> object creation)
>
> Don't ask for a particular situation. It is yet not quite known, I am just
> writing some abstract foundational classes for our company, which may be
> used in different situations.

Even if the class is abstract there is a functionality it defines. In
particular its interface all implementations (descendants, members of the
class, derived types, name them as you wish) must implement. If existence
of the row is a part of that functionality shared by all descendants and
one all clients will rely on then you must check it.

Regarding referential objects there exist designs which allow illegal/null
references and those that do not. The later are preferable, but not always
possible.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-...