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

Business object design
Schmidtty
There are three options as far as I can see.
The best one is not to store nulls in the database. Where possible/logical, this is always the best choice
Second - add properties that check if the value was null that return a bool. This, along with setting 'magic numbers' for null, is a hack IMO
Finally, you could define a struct, which you return, which does what INullable does, it has a bool to say if the value is null, and if it isn't also contains the value itself.
Joseph Molnar
Guruparan
I agree with the first option but sometimes there are fields where values are unkown (i.e. subscription end date). One can store min date or max date, but this is misleading for the users.
The problem with second approach is every property will need two property and the class will be clumsy.
Have you implemented something like this before
Deepak83
In .NET 2.0, the problem is solved by using nullable types. For example, instead of int, use int , which is a shortcut for INullable<int> and it means your return value can be an int, or it can be null.
http://wesnerm.blogs.com/net_undocumented/2004/05/nullable_types.html
Balakrishnan Muthubabu
I agree on both points. Solution one is not always possible, and solution 2 is a hack.
The objects that I return on the main DB project I work on all are created using handles, and we have a handle class which is constructed from an object or string, although it really needs to find a number from either of those items, and has an IsNull property. So, my code works around solution three - if I need to expose an object, I expose the handle to create it, and when I retrieve a handle, I check it's IsNull property before using it.
Gerry Rempel
Hi cgraus:
Will appreciate if you could elaborate on the solution you mentioned (if possible few code snippets).