Best Practice when trying to Load object from Data Layer when it doesn't find it

  • Thread starter Thread starter TS
  • Start date Start date
T

TS

If I was to have my biz layer ask the data layer to load a particular object
based on key field info i pass to it, and it cannot create the object
becaues it isnt' in the Db, should the data layer pass back an exception or
a null reference?

if an exception, i would imagine i should catch this and display a message
to the user?

thanks
 
TS said:
If I was to have my biz layer ask the data layer to load a particular
object based on key field info i pass to it, and it cannot create the
object becaues it isnt' in the Db, should the data layer pass back an
exception or a null reference?

if an exception, i would imagine i should catch this and display a message
to the user?

I would prefer some non-exception indicator that the record was not found.
If you want to return a null object, that would work. Don't forget to check
for null as you will get a null reference exception if you try to access the
object without checking for a null object reference first.

You might also consider using some other indicator such as a status code or
bool value that indicates whether or not the object was successfully created
from the DB.
 
Exceptions should be "Exceptional"

Thus, if this is something you except to happen, then return a null.
 
If I was to have my biz layer ask the data layer to load a particular object
based on key field info i pass to it, and it cannot create the object
becaues it isnt' in the Db, should the data layer pass back an exception or
a null reference?

if an exception, i would imagine i should catch this and display a message
to the user?

thanks

I guess it would depend on your application design. If NOT finding a
record with a given primary key is part of the application design i.e.
is part of the functionality then perhaps a null would suffice.

Personally I would throw an exception and handle that in the UI
 
TS said:
If I was to have my biz layer ask the data layer to load a particular
object based on key field info i pass to it, and it cannot create the
object becaues it isnt' in the Db, should the data layer pass back an
exception or a null reference?

I can think of a few different approaches to this.

1) null reference - you check that the object == null and handle accordingly
2) throw an exception - you catch the exception and handle accordingly
3) return a Null Object - you encapsulate the behavior of what to do when
the ID was not found into the object itself.
4) return a default object - you use another object as the default object
when the ID was not found
5) create an object with default values - you create an object with default
values and return this

What is the reason that the ID was not found in the database? Is this
something critical or something that is expected and you want to handle this
gracefully? If it is expected then do not throw an exception.

PS

For example
 
Hi TS,

In my opinion, if you're returning some status code, it will be better if
you can have some status code for database not found. If you return the
object directly to the biz layer, I would suggest you throw an exception,
which makes difference to the certain data is not found.

If anything is unclear, please feel free to let me know.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
TS said:
this is something that happens infrequently in production system

But that doesn't answer the important question. Is it truly unexpected, or
exceptional, behavior when the record isn't found. If it's not then you
shouldn't throw an exception. If it is, then that designed has more merit.
 
TS said:
this is something that happens infrequently in production system

And how does it happen? I mean how is it that the application manages to
know a key that does NOT exist in the database? Where does it get the keys
from?
 
Ok, what happens is i am getting an exception in production for when 2
browsers have a web page open showing a specific record. The one browser
deletes the record. The other browser then tries to do something with it
(delete it, edit it, save it, view it, etc) and the code behind tries to
load that record to save the new value into it and it no longer exists. Its
a rare thing but does happen. The methods are set up to throw exceptions if
a record isn't found based on the key values that are sent to it (id of the
record in the DB).

thanks for your posts guys!!!
 
TS said:
Ok, what happens is i am getting an exception in production for when 2
browsers have a web page open showing a specific record. The one browser
deletes the record. The other browser then tries to do something with it
(delete it, edit it, save it, view it, etc) and the code behind tries to
load that record to save the new value into it and it no longer exists.
Its a rare thing but does happen. The methods are set up to throw
exceptions if a record isn't found based on the key values that are sent
to it (id of the record in the DB).

So it's a concurrency issue. Are you also handling the update/update
scenario? This one won't throw exceptions, unless you allow updating of
primary/unique keys, but can lead to data loss.

This is one where the more users you have, the more likely you are to run
into this. You need to employ a mechanism to make sure that your data is
fresh. You need to decide how you want to handle this.

In your scenario I would not use exceptions but instead use return codes.
 
TS said:
Ok, what happens is i am getting an exception in production for when 2
browsers have a web page open showing a specific record. The one browser
deletes the record. The other browser then tries to do something with it
(delete it, edit it, save it, view it, etc) and the code behind tries to
load that record to save the new value into it and it no longer exists.
Its a rare thing but does happen. The methods are set up to throw
exceptions if a record isn't found based on the key values that are sent
to it (id of the record in the DB).

thanks for your posts guys!!!

You have a concurrecy issue then. With optimistic locking this is to be
expected (and is not an exception). And it is not just on deletes that you
need to be concerned. With optimistic locking you also need to check that
the underlying record was not changed in the meantime by another user
otherwise changes made by one user are written over by another user (aka
inconsisten read). Therefore you need to handle both deleted and edited
objects accordingly. Alternatively you can use a pessimistic locking scheme
so that no one can edit/delete any object that is being edited/deleted by
another user.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top