DataMapper and Decimals (Numbers)

DataMapper and Decimals (Numbers)

Old forum URL: forums.lhotka.net/forums/t/8121.aspx


Wbmstrmjb posted on Wednesday, December 09, 2009

I am surprised that this is not brought up in another thread, but I was unable to find it.

How do you guys handle DataMapper.Map mapping "0" back to decimals (numbers) instead of "null" if there is no value? Seems like you would not want "0" being set because "0" is a real value and not the same as null. Haven't others crossed this issue before?

Thanks,
Mike

tmg4340 replied on Wednesday, December 09, 2009

One of the tenets of CSLA is that in general, NULL and 0 are the same thing.  If you read through all the books (and various forum posts), you'll see that's how Rocky built the framework - NULL's are converted to a type-appropriate default value.  If you do have a need to distringuish between NULL and a value, you can do it - but the framework doesn't do it for you by default.

In terms of DataMapper, I can't offer a specific solution - I haven't looked at the code in a long time, and I don't use it.  One possibility that comes to mind is to use a decimal?, but I don't know whether DataMapper is smart enough to handle that.  In any event, I think you'll encounter situations like this throughout CSLA.

HTH

- Scott

Wbmstrmjb replied on Wednesday, December 09, 2009

The "0" is null is interesting, but then how do you handle "0" as a value? If I have a bank account balance of "0" that's different than having an account with the balance not yet set to a value. How do you distinguish between "0" and "not set"?

RockfordLhotka replied on Wednesday, December 09, 2009

tmg4340:

One of the tenets of CSLA is that in general, NULL and 0 are the same thing. 

Let's be careful around this topic.

The SafeDataReader exists to "de-null" values. It is one tiny class in a large framework. The rest of the framework doesn't care what you do with your null values.

Personally, one of my pet peeves is the misuse of null in databases. In my personal experience, almost every database has many columns that allow null values where there's absolutely no business rationale for such a thing. Nor is the column a foreign key, or an empty date - the only two technical reasons for allowing a null value.

Due to all the crappy databases out there, it was (and is) common practice to write data access code to check for, and remove, null values. SafeDataReader just makes this easier.

But you must be aware that you don't need to use SafeDataReader. It is merely a wrapper around your real datareader object. Nothing stops you from using your real datareader that you got from ADO.NET - and thereby getting at the null values and using them as you see fit.

I would hope that "you see fit" means there are specific cases where business users care about the difference between a value that was never entered, and a value that is empty ("", 0, false, etc). That (and the two technical reasons listed above) are the only reasons your database should allow null values in a column - and therefore are the only reasons your app should have to deal with null values.

Wbmstrmjb replied on Thursday, December 10, 2009

I agree to an extent, but I think there are plenty of real uses for null (or "not set") over a default value for every column (0, false, "", etc).

Usually an amount should be something other than $0.00, so I'll concede that even though I find it odd to have a textbox with $0.00 in it before a user enters a value.

But in the case of true/false with a default of false (or true for that matter), "not set" is very different than false. Answers to questions should be null until answered. How would one know looking at an answer whether the person answered false or there just hasn't been an answer entered yet? That seems like a very common and very real need for nulls. And in the case of needing nulls, how does one map those values with DataMapper.Map? Is it just not possible?

Thanks.

RockfordLhotka replied on Thursday, December 10, 2009

That's what I'm saying though. If there is a _business need_ to
differentiate between a value having not been set, and a "real value" then
that's fine - that's the point of null.

In that case though, SafeDataReader isn't useful, because you actually care
about the null value, and so you should use whatever datareader you get from
executing your command.

Wbmstrmjb replied on Thursday, December 10, 2009

Yes and no. We extended SafeDataReader and added SmartDecimal so that "decimal" can be null. :)

Wbmstrmjb replied on Thursday, December 10, 2009

That you for your insight. We will "map" it another way.

RockfordLhotka replied on Thursday, December 10, 2009

In any case, we've veered off your original question.

DataMapper is pretty dumb - it just copies values from property to property.

But this comes back to what I was saying earlier. If you really care about null values from a business perspective, then that concept must (by definition) be expressed from the database all the way through to the UI.

Typically DataMapper is used to map web UI values back into the business object. So if your UI represents the null concept, you should be able to map it - though perhaps not with DataMapper.

I say this, because different UI technologies represent null in the UI in different ways (or not at all - in which case you sometimes need to invent the concept). Microsoft has gotten better about this over the past few years, at least on the Windows side. I do little on the web side, so I don't know if Web Forms expresses null values well or not...

Copyright (c) Marimer LLC