473,395 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1800
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
1
by: Herve MAILLARD | last post by:
Hi, I have to write a software doing the following : - Load a file (containing data) Data will be display in a Treeview and are typed as following Equipement Bloc Tag It could have for each...
14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
3
by: cbrown | last post by:
I am rebuilding an existing application that relies on an SQL DB. The app is a scheduling/employee management program. My question pertains to best practices in dotnet and database. I use a 3...
13
by: Alan Silver | last post by:
Hello, MSDN (amongst other places) is full of helpful advice on ways to do data access, but they all seem geared to wards enterprise applications. Maybe I'm in a minority, but I don't have those...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
13
by: G | last post by:
Hello, Looking for opinions on a fairly simple task, new to ASP.net (C#) and want to make sure I do this as efficiently as possible. I have a web based form, and I need to run some SQL before...
7
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id -...
2
by: Gabriel | last post by:
Hello, I'm looking for documentation with "Best Practice" for ASP.NET application In which case use Connected or Disconnected mode Typed dataset or not ? I didn'd find anything pertinent...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.