468,290 Members | 2,066 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,290 developers. It's quick & easy.

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

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
Nov 13 '06 #1
12 1582
TS wrote:
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.
--
Tom Porterfield

Nov 13 '06 #2

Exceptions should be "Exceptional"

Thus, if this is something you except to happen, then return a null.


"Tom Porterfield" <tp******@mvps.orgwrote in message
news:ek**************@TK2MSFTNGP03.phx.gbl...
TS wrote:
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.
--
Tom Porterfield

Nov 13 '06 #3
An exceptional comment from an exceptional guy.

:-)

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"sloan" wrote:
>
Exceptions should be "Exceptional"

Thus, if this is something you except to happen, then return a null.


"Tom Porterfield" <tp******@mvps.orgwrote in message
news:ek**************@TK2MSFTNGP03.phx.gbl...
TS wrote:
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.
--
Tom Porterfield


Nov 13 '06 #4
PS

"TS" <ma**********@nospam.nospamwrote in message
news:uA**************@TK2MSFTNGP02.phx.gbl...
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
>
if an exception, i would imagine i should catch this and display a message
to the user?
Nov 13 '06 #5
Rad
On Mon, 13 Nov 2006 14:39:43 -0600, "TS" <ma**********@nospam.nospam>
wrote:
>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
Nov 13 '06 #6
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 14 '06 #7
TS
this is something that happens infrequently in production system
"PS" <ec***********@hotmail.comwrote in message
news:uX**************@TK2MSFTNGP03.phx.gbl...
>
"TS" <ma**********@nospam.nospamwrote in message
news:uA**************@TK2MSFTNGP02.phx.gbl...
>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
>>
if an exception, i would imagine i should catch this and display a
message to the user?

Nov 14 '06 #8
TS wrote:
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.
--
Tom Porterfield

Nov 14 '06 #9
PS

"TS" <ma**********@nospam.nospamwrote in message
news:uT**************@TK2MSFTNGP02.phx.gbl...
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?
>

"PS" <ec***********@hotmail.comwrote in message
news:uX**************@TK2MSFTNGP03.phx.gbl...
>>
"TS" <ma**********@nospam.nospamwrote in message
news:uA**************@TK2MSFTNGP02.phx.gbl...
>>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
>>>
if an exception, i would imagine i should catch this and display a
message to the user?

Nov 14 '06 #10
TS
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!!!

"PS" <ec***********@hotmail.comwrote in message
news:ee**************@TK2MSFTNGP03.phx.gbl...
>
"TS" <ma**********@nospam.nospamwrote in message
news:uT**************@TK2MSFTNGP02.phx.gbl...
>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?
>>

"PS" <ec***********@hotmail.comwrote in message
news:uX**************@TK2MSFTNGP03.phx.gbl...
>>>
"TS" <ma**********@nospam.nospamwrote in message
news:uA**************@TK2MSFTNGP02.phx.gbl...
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
if an exception, i would imagine i should catch this and display a
message to the user?


Nov 14 '06 #11
TS wrote:
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.
--
Tom Porterfield

Nov 14 '06 #12
PS

"TS" <ma**********@nospam.nospamwrote in message
news:eP**************@TK2MSFTNGP03.phx.gbl...
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.
>
"PS" <ec***********@hotmail.comwrote in message
news:ee**************@TK2MSFTNGP03.phx.gbl...
>>
"TS" <ma**********@nospam.nospamwrote in message
news:uT**************@TK2MSFTNGP02.phx.gbl...
>>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?
>>>

"PS" <ec***********@hotmail.comwrote in message
news:uX**************@TK2MSFTNGP03.phx.gbl...

"TS" <ma**********@nospam.nospamwrote in message
news:uA**************@TK2MSFTNGP02.phx.gbl...
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

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

Nov 14 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Herve MAILLARD | last post: by
3 posts views Thread by cbrown | last post: by
13 posts views Thread by G | last post: by
7 posts views Thread by Steve | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.