473,659 Members | 2,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1817
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 "Exceptiona l"

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


"Tom Porterfield" <tp******@mvps. orgwrote in message
news:ek******** ******@TK2MSFTN GP03.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 "Exceptiona l"

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


"Tom Porterfield" <tp******@mvps. orgwrote in message
news:ek******** ******@TK2MSFTN GP03.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**********@n ospam.nospamwro te in message
news:uA******** ******@TK2MSFTN GP02.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**********@n ospam.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.comwrot e in message
news:uX******** ******@TK2MSFTN GP03.phx.gbl...
>
"TS" <ma**********@n ospam.nospamwro te in message
news:uA******** ******@TK2MSFTN GP02.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**********@n ospam.nospamwro te in message
news:uT******** ******@TK2MSFTN GP02.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.comwrot e in message
news:uX******** ******@TK2MSFTN GP03.phx.gbl...
>>
"TS" <ma**********@n ospam.nospamwro te in message
news:uA******* *******@TK2MSFT NGP02.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
9243
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 C++. I find my self sometimes, trying Object app = Object(); Object *app = Object(); Object app = new Object();
1
2107
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 Equipement, many blocs and for each bloc, many tags.
14
3129
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... (both are "Pascal Case" with no leading or trailing qualifiers). For example... I'll be modelling something, e.g. a computer, and I'll
3
1809
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 tier model and have custom classes for things like Employees. What comes to question is when I load an Employee from my DB, I populate the properties of the object with the data in the dataset. When I want to view the employee on a form, i bind...
13
3101
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 sorts of clients. Mine are all small businesses whose sites will never reach those sorts of scales. I deal with businesses whose sites get maybe a few hundred visitors per day (some not even that much) and get no more than ten orders per day....
16
2794
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 efficient navigating within Visual Studio 2005. Let's say your project (or solution) has dozens of forms and hundreds or even thousands of routines. Two Questions: 1) BUILT-IN to Visual Studio 2005. What ideas do you have to quickly
13
2563
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 submit, which determines exactly where to send the form contents. The table of "receipients" could contain in the region of 3,500 recipients but is more likely to contain up to 1,000. Table structure:
7
3577
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 - FirstName - LastName - CompanyId (many-to-one )
2
3322
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
8427
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8850
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.