473,395 Members | 1,675 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.

Business Objects

I have the following sample class where the property value will come from the
database and in the database the value can be null. I believe this is a
common problem if one wants to design bisuness objects. I have read about the
concepts in many books but no one talks about to solve this problem.

In .Net 2.0 there is a solution, but since this is a common problem, I was
wondering how other have solved it before.

public class SampleClass
{
private string stringProp;
private int intProp;
private DateTime dateProp;

public SampleClass()
{
//
// TODO: Add constructor logic here
//
}

public int IntProp
{
get
{
return intProp;
}
set
{
intProp = value;
}
}

public string StringProp
{
get
{
return stringProp;
}
set
{
stringProp = value;
}
}

public DateTime DateProp
{
get
{
return dateProp;
}
set
{
dateProp = value;
}
}
}

Feb 17 '06 #1
4 1283
a given database column value can be null whereas, often, the equivalent CLR
datatype value is never null, is three quarters of the battle.

The other quarter is for you to determine what value you consider to be null
for a given FCL datatype when the database column it is mapped to is null.

It is difficult to determine a blanket policy for this on a type, rather it
usually must be determined on a database column by database column basis.

Let's take a database table design (that matches SampleClass) as an example.

create table SampleClass (
stringProp varchar(50),
intProp int,
dateProp datetime
)

If you add a row to the table from an instance of SampleClass that has had
no values assigned, you will get:

an empty string,
0, and
0001-01-01 00:00:00

If, for example, you determine that, for SampleClass.StringProp, a null is
represented by an empty string, for SampleClass.IntProp, a null is
represented by 0 and for SampleClass.DateProp, a null is represented by
DateTime.MinValue then each time you update the relevant column in the
database you need to test for the 'null' value and use DBNull.Value where
appropriate. This example dictates that the database column
SampleClass.IntProp can never contain 0.

If you add a column to the database table:

intProp2 int,

where intProp2 can contain 0, you have a different issue. You cannnot use 0
to represent a null value for SampleClass.IntProp2 so you have to use
something else, perhaps -1 if intProp2 cannot contain a negative value.

As you can see, the 'gotchas' can continue ad infinitum, but, as I said
earlier, recognizing the issue is three quartes of the battle.
"pothik" <po****@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
I have the following sample class where the property value will come from
the
database and in the database the value can be null. I believe this is a
common problem if one wants to design bisuness objects. I have read about
the
concepts in many books but no one talks about to solve this problem.

In .Net 2.0 there is a solution, but since this is a common problem, I was
wondering how other have solved it before.

public class SampleClass
{
private string stringProp;
private int intProp;
private DateTime dateProp;

public SampleClass()
{
//
// TODO: Add constructor logic here
//
}

public int IntProp
{
get
{
return intProp;
}
set
{
intProp = value;
}
}

public string StringProp
{
get
{
return stringProp;
}
set
{
stringProp = value;
}
}

public DateTime DateProp
{
get
{
return dateProp;
}
set
{
dateProp = value;
}
}
}

Feb 17 '06 #2
Hi Stephany:

Thanks for your reply. I have seen this solution and myself used it. But the
problem with this approach is you will have data in tables, which are not
actual data and in many controls (where the control displays the data
directly) this will be confusing for the users.

Since everyone talks about business objects, I thought there many be some
other solutions. Is this how all the business objects are implemented?

"Stephany Young" wrote:
a given database column value can be null whereas, often, the equivalent CLR
datatype value is never null, is three quarters of the battle.

The other quarter is for you to determine what value you consider to be null
for a given FCL datatype when the database column it is mapped to is null.

It is difficult to determine a blanket policy for this on a type, rather it
usually must be determined on a database column by database column basis.

Let's take a database table design (that matches SampleClass) as an example.

create table SampleClass (
stringProp varchar(50),
intProp int,
dateProp datetime
)

If you add a row to the table from an instance of SampleClass that has had
no values assigned, you will get:

an empty string,
0, and
0001-01-01 00:00:00

If, for example, you determine that, for SampleClass.StringProp, a null is
represented by an empty string, for SampleClass.IntProp, a null is
represented by 0 and for SampleClass.DateProp, a null is represented by
DateTime.MinValue then each time you update the relevant column in the
database you need to test for the 'null' value and use DBNull.Value where
appropriate. This example dictates that the database column
SampleClass.IntProp can never contain 0.

If you add a column to the database table:

intProp2 int,

where intProp2 can contain 0, you have a different issue. You cannnot use 0
to represent a null value for SampleClass.IntProp2 so you have to use
something else, perhaps -1 if intProp2 cannot contain a negative value.

As you can see, the 'gotchas' can continue ad infinitum, but, as I said
earlier, recognizing the issue is three quartes of the battle.
"pothik" <po****@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
I have the following sample class where the property value will come from
the
database and in the database the value can be null. I believe this is a
common problem if one wants to design bisuness objects. I have read about
the
concepts in many books but no one talks about to solve this problem.

In .Net 2.0 there is a solution, but since this is a common problem, I was
wondering how other have solved it before.

public class SampleClass
{
private string stringProp;
private int intProp;
private DateTime dateProp;

public SampleClass()
{
//
// TODO: Add constructor logic here
//
}

public int IntProp
{
get
{
return intProp;
}
set
{
intProp = value;
}
}

public string StringProp
{
get
{
return stringProp;
}
set
{
stringProp = value;
}
}

public DateTime DateProp
{
get
{
return dateProp;
}
set
{
dateProp = value;
}
}
}


Feb 17 '06 #3
I don't understand what you mean by 'you will have data in tables ...'.

If YOU decide that zero (0) is to be used for a property in your class to
indicate NULL in the 'mapped' database column, then it is imperative that,
when you update the database, YOU substitute the 0 with DBNull.Value and
that when you read from the database YOU substitute DBNull.Value with 0.

This way, when the database column is supposed to contain NULL, then it
does.
"pothik" <po****@discussions.microsoft.com> wrote in message
news:3F**********************************@microsof t.com...
Hi Stephany:

Thanks for your reply. I have seen this solution and myself used it. But
the
problem with this approach is you will have data in tables, which are not
actual data and in many controls (where the control displays the data
directly) this will be confusing for the users.

Since everyone talks about business objects, I thought there many be some
other solutions. Is this how all the business objects are implemented?

"Stephany Young" wrote:
a given database column value can be null whereas, often, the equivalent
CLR
datatype value is never null, is three quarters of the battle.

The other quarter is for you to determine what value you consider to be
null
for a given FCL datatype when the database column it is mapped to is
null.

It is difficult to determine a blanket policy for this on a type, rather
it
usually must be determined on a database column by database column basis.

Let's take a database table design (that matches SampleClass) as an
example.

create table SampleClass (
stringProp varchar(50),
intProp int,
dateProp datetime
)

If you add a row to the table from an instance of SampleClass that has
had
no values assigned, you will get:

an empty string,
0, and
0001-01-01 00:00:00

If, for example, you determine that, for SampleClass.StringProp, a null
is
represented by an empty string, for SampleClass.IntProp, a null is
represented by 0 and for SampleClass.DateProp, a null is represented by
DateTime.MinValue then each time you update the relevant column in the
database you need to test for the 'null' value and use DBNull.Value where
appropriate. This example dictates that the database column
SampleClass.IntProp can never contain 0.

If you add a column to the database table:

intProp2 int,

where intProp2 can contain 0, you have a different issue. You cannnot use
0
to represent a null value for SampleClass.IntProp2 so you have to use
something else, perhaps -1 if intProp2 cannot contain a negative value.

As you can see, the 'gotchas' can continue ad infinitum, but, as I said
earlier, recognizing the issue is three quartes of the battle.
"pothik" <po****@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
>I have the following sample class where the property value will come
>from
>the
> database and in the database the value can be null. I believe this is a
> common problem if one wants to design bisuness objects. I have read
> about
> the
> concepts in many books but no one talks about to solve this problem.
>
> In .Net 2.0 there is a solution, but since this is a common problem, I
> was
> wondering how other have solved it before.
>
> public class SampleClass
> {
> private string stringProp;
> private int intProp;
> private DateTime dateProp;
>
> public SampleClass()
> {
> //
> // TODO: Add constructor logic here
> //
> }
>
> public int IntProp
> {
> get
> {
> return intProp;
> }
> set
> {
> intProp = value;
> }
> }
>
> public string StringProp
> {
> get
> {
> return stringProp;
> }
> set
> {
> stringProp = value;
> }
> }
>
> public DateTime DateProp
> {
> get
> {
> return dateProp;
> }
> set
> {
> dateProp = value;
> }
> }
> }
>


Feb 17 '06 #4
Please check out nullable types in .Net 2.0. DateTime? can be null but
DateTime cannot. We have switched are data access code to use this technique.

macfarmw
"pothik" wrote:
I have the following sample class where the property value will come from the
database and in the database the value can be null. I believe this is a
common problem if one wants to design bisuness objects. I have read about the
concepts in many books but no one talks about to solve this problem.

In .Net 2.0 there is a solution, but since this is a common problem, I was
wondering how other have solved it before.

public class SampleClass
{
private string stringProp;
private int intProp;
private DateTime dateProp;

public SampleClass()
{
//
// TODO: Add constructor logic here
//
}

public int IntProp
{
get
{
return intProp;
}
set
{
intProp = value;
}
}

public string StringProp
{
get
{
return stringProp;
}
set
{
stringProp = value;
}
}

public DateTime DateProp
{
get
{
return dateProp;
}
set
{
dateProp = value;
}
}
}

Feb 20 '06 #5

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

Similar topics

5
by: Shibu | last post by:
Hi, I have a situation where I need to convert business objects to a flat table. The reverse is also required. I am using c# and Oracle ODP. I am looking for an easier method to do the below...
25
by: Stuart Hilditch | last post by:
Hi all, I am hoping that someone with some experience developing nTier apps can give me some advice here. I am writing an nTier web app that began with a Data Access Layer (DAL), Business...
2
by: headware | last post by:
I am new to 3-tiered design and am in the process of designing an application that will work with ASP.NET as well as Windows Forms (and possibly with a PDA of some sort down the road). My question...
5
by: G. Stewart | last post by:
The word "Business" in the term implies some sort of commercial aspects or connotations. But from what I can see, that is not necesserially the case at all? So what is the reasoning behind the...
8
by: Keith-Earl | last post by:
Okay, looking for a Best Practice. We are building a classic three tier app in VB.NET. When we load up a WebForm we have access to very useful objects such as the Session object. We frequently...
18
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
1
by: Nemisis | last post by:
hi guys, Currently converting an old classic asp system to a OOP asp.net application. We are building the new application using a 3 tier arcitecture and i was wondering about the following. ...
25
by: Penelope Dramas | last post by:
Hello, I'm in a front of very serious .net redesign/rewrite of an old VB6 application. I had been asked to make it .NET 2.0 and would like to ask couple of questions regarding data access as...
2
by: grawsha2000 | last post by:
Greetings, I am developing this N-tier business app. The problem I'm facing is when I try to pass business objects (employees, dept..etc) from business tier to data tier,i.e., the add method in...
2
by: Chris Zopers | last post by:
Hello, I would like to know what's the best way to implement a business logic layer between my user interface and my database. I would say I'd make a dll-project for the business logic layer...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.