DNN Data Access Policy (ii)

Source: Internet
Author: User
Tags abstract constant dnn

Air Handling

Each data access system has a special construct to handle the field values that are not explicitly specified.

In most relational database management systems, this construct is known as a null value. From an application perspective, passing null values at the presentation layer and the data access layer is an architectural challenge. This is because the presentation layer must be abstracted from the specific information of the database, and the presentation layer must be able to express the description when a property value is not explicitly specified. In fact this is quite complicated, the data types of the. NET framework cannot automatically convert null values returned from the database (an exception is thrown if you try to assign the value directly). In addition, each data store has its own properties to implement null. The only reasonable solution is to create an abstract transport service that encodes/decodes null values between the layers of the application.

At first glance, you may think that using the "nothing" keyword in vb.net can be a good task for the transport service. Unfortunately, the survey shows that the data types of the. NET framework itself are not as good as expected when dealing with "nothing." Although a property assigned to nothing does not throw an exception, the value of this property is actually very dependent on its data type (String = Nothing, Date = date.minvalue, Integer = 0, Boolean = False, and so on) and its own The results of the isnothing () function are not consistent (compatible) results.

In DNN, we created a generic class to handle the problem of NULL, which unified management of the null problem for each layer of the application. Use a constant in the application to describe the null situation for each data type, and then convert the constant to the actual null value in various data store implementations. This class contains various methods to abstract the physical details of the null transformation service from the application.

* Remember, this class is used only if the database field allows null values. Also remember that this class requires a consistent data type between the DAL and the BLL layer (for example, the data type of an attribute field in a BLL information class must match the data type of the parameter passed by the DAL data provider).

Public Class Null

' Define application encoded null values
Public Shared ReadOnly Property Nullinteger () as Integer
Get
Return-1
End Get
End Property
Public Shared ReadOnly Property nulldate () as Date
Get
Return Date.minvalue
End Get
End Property
Public Shared ReadOnly Property nullstring () as String
Get
Return ""
End Get
End Property
Public Shared ReadOnly Property Nullboolean () as Boolean
Get
Return False
End Get
End Property

' Sets a field to a application encoded null value (used in presentation layer)
Public Shared Function SetNull (ByVal objfield as Object) as Object
If TypeOf Objfield is Integer Then
SetNull = Nullinteger
ElseIf TypeOf Objfield is Date Then
SetNull = Nulldate
ElseIf TypeOf Objfield is String Then
SetNull = nullstring
ElseIf TypeOf Objfield is Boolean Then
SetNull = Nullboolean
Else
Throw New NullReferenceException ()
End If
End Function

' Sets a field to a application encoded null value (used in BLL layer)
Public Shared Function SetNull (ByVal objpropertyinfo as PropertyInfo) as Object
Select Case ObjPropertyInfo.PropertyType.ToString
Case "System.Int16", "System.Int32", "System.Int64", "System.Single", "System.Double", "System.Decimal"
SetNull = Nullinteger
Case "System.DateTime"
SetNull = Nulldate
Case "System.String", "System.Char"
SetNull = nullstring
Case "System.Boolean"
SetNull = Nullboolean
Case Else
Throw New NullReferenceException ()
End Select
End Function

' Convert an application encoded NULL value to a database null value (used in DAL)
Public Shared Function Getnull (ByVal objfield As Object, ByVal Objdbnull as Object) as Object
Getnull = Objfield
If TypeOf Objfield is Integer Then
If objfield = Nullinteger Then
Getnull = Objdbnull
End If
ElseIf TypeOf Objfield is Date Then
If objfield = nulldate Then
Getnull = Objdbnull
End If
ElseIf TypeOf Objfield is String Then
If objfield = nullstring Then
Getnull = Objdbnull
End If
ElseIf TypeOf Objfield is Boolean Then
If objfield = Nullboolean Then
Getnull = Objdbnull
End If
Else
Throw New NullReferenceException ()
End If
End Function

' Checks if a field contains an application encoded NULL value
Public Shared Function IsNull (ByVal objfield as Object) as Boolean
If objfield = setnull (objfield) Then
IsNull = True
Else
IsNull = False
End If
End Function

End Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.