Business Objects

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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;
}
}
}
 
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.
 
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?
 
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.
 
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
 
Back
Top