Object Models and NULL

G

Guest

I posted this a couple of days ago, but it was buried with another question.

How do people deal with NULL values in an object model?

Consider the following example often used. I can't see anyway to indicate
an age is undefined. So if I get a null from the database what do I store
here and when placing it on the screen, how do I know to put nothing (as
opposed to 0). Is there a methodology people use? Null indicator variable,
Nullable type, etc. Do I just write compound if statements to test and
determine what to use everywhere? Seems like a very common problem many
other people must be solving in fashion.

Public Class Customer
Private _age As Integer
Public Property Age() As Integer
Get
Return _age
End Get
Set(ByVal value As Integer)
_age = value
End Set
End Property
End Class
 
K

Karl Seguin [MVP]

Nullable types in 2.0, which are a real let down in the fundamental mapping
way, is the best way to deal with the problem.

It's total crap that you can't assign DbNull.Value to a nullable type nad
have it just work. You still need to if value is DbNull.Value in your
mapping layer (both ways), but alteast once that's done, you have access to
x.HasValue for all primitive types (int/bools/blahs)

Assuming you can't move to 2.0...there's no magic solution. On approach is
to not allow nulls for those fields in your database - which makes the
mapping process a lot easier, but isn't practical in all cases. I've used
things like constants assigned to Int32.MinValue - which is a majob problem
waiting to happen.

Just some thoughts :)

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top