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;
}
}
}