Web Log di Adrian Florea

"You know you've achieved perfection in design, not when you have nothing more to add, but when you have nothing more to take away." Antoine de Saint-Exupery
posts - 440, comments - 2715, trackbacks - 3944

My Links

Archives

Post Categories

Image Galleries

.RO Blogs

.RO People

.RO Sites

Blogs

Furls

Links

vinCitori

SafeDataReader e "Lettura dei dati e DbNull"

Un paragrafo (p. 72) dal libro "Expert One-on-One Visual Basic .NET Business Objects" di Rockford Lhotka (MVP, Regional Director e Software Legend - cosa gli manca?) che viene a completare quello che è stato detto in questo post "Lettura dei dati e DbNull" del nostro M.rkino (MVP anche lui):

"Most of the time, we don't care about the difference between a null value and an empty value (such as an empty string or a zero), but databases often do. When we're retrieving data from a database, we need to handle the occurence of unexpected null values with code such as this:

If dr.IsDBNull(idx) Then
  myValue = ""
Else
  myValue = dr.GetString(idx)
End If

Clearly, doing this over and over again, throughout our application, can get very tiresome. One solution is to fix the database so that it doesn't allow nulls where they provide no value, but that is often impractical for various reasons.

This is one of my pet peeves. Allowing nulls in a column where we care about the difference between a value that was never entered and the empty value ("", or 0, or whatever) is fine. Allowing nulls in a column where we don't care about the difference merely complicates our code to no good purpose, decreasing developer productivity and increasing maintenance costs.

As a more general solution, we can create a utility class that uses SqlDataReader in such a way that we never have to worry about null values again. Unfortunately, the SqlDataReader class is not inheritable, so we can't subclass it directly, but we can wrap it using containment and delegation. The result is that our data access code works the same as always, except that we never need to write checks for null values. If a null value shows up, SafeDataReader will automatically convert it to an appropiate empty value.

Obviously, if we do care about the difference between a null and an empty value, we just use a regular SqlDataReader to retrieve our data."

La classe DataRecordSupport di M.rkino è quasi identica alla classe CSLA.Data.SafeDataReader di Rocky Lhotka - ma sono sicuro che non l'ha copiata da lì :-)

Print | posted on giovedì 19 agosto 2004 15:11 | Filed Under [ Carillon .NET ]

Powered by:
Powered By Subtext Powered By ASP.NET