Null Class as Utility
Utility 로 쓰면 어떨까 해서 올립니다.
1. Utility 로 사용할 Null Class
첫 소스는 codeproject 에서 가져왔는데요
조금 조금씩 수정했습니다.
코드를 보시면 이걸 왜 이렇게 했을까… 라는 생각을 갖게 하는 부분들이 있습니다. ^^
필요하신 부분 있으시면 수정해서 사용하시면 좋을 듯합니다.
이런 코드도 있더라 정도로 생각해 두시면… ^^
이런식으로 자신만의 함수들을 따로 만들어 놓으시면
자신만의 프래임웍 유틸리티를 만들수 있겠죠? ^^
2. Code
Code Snippet
- Imports System
- Imports System.Reflection
- Public Class Null
- ‘ define application encoded null values
- Public Shared ReadOnly Property NullShort() As Short
- Get
- Return -1
- End Get
- End Property
- Public Shared ReadOnly Property NullInteger() As Integer
- Get
- Return -1
- End Get
- End Property
- Public Shared ReadOnly Property NullSingle() As Single
- Get
- Return Single.MinValue
- End Get
- End Property
- Public Shared ReadOnly Property NullDouble() As Double
- Get
- Return Double.MinValue
- End Get
- End Property
- Public Shared ReadOnly Property NullDecimal() As Decimal
- Get
- Return Decimal.MinValue
- 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
- Public Shared ReadOnly Property NullGuid() As Guid
- Get
- Return Guid.Empty
- End Get
- End Property
- ‘ sets a field to an application encoded null value ( used in BLL layer )
- Public Shared Function SetNull(ByVal objValue As Object, ByVal objField As Object) As Object
- If IsDBNull(objValue) Then
- If TypeOf objField Is Short Then
- Return NullShort
- ElseIf TypeOf objField Is Integer Then
- Return NullInteger
- ElseIf TypeOf objField Is Single Then
- Return NullSingle
- ElseIf TypeOf objField Is Double Then
- Return NullDouble
- ElseIf TypeOf objField Is Decimal Then
- Return NullDecimal
- ElseIf TypeOf objField Is Date Then
- Return NullDate
- ElseIf TypeOf objField Is String Then
- Return NullString
- ElseIf TypeOf objField Is Boolean Then
- Return NullBoolean
- ElseIf TypeOf objField Is Guid Then
- Return NullGuid
- Else ‘ complex object
- Return Nothing
- End If
- Else ‘ return value
- Return objValue
- End If
- End Function
- ‘ sets a field to an 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”
- Return NullShort
- Case “System.Int32”, “System.Int64”
- Return NullInteger
- Case “System.Single”
- Return NullSingle
- Case “System.Double”
- Return NullDouble
- Case “System.Decimal”
- Return NullDecimal
- Case “System.DateTime”
- Return NullDate
- Case “System.String”, “System.Char”
- Return NullString
- Case “System.Boolean”
- Return NullBoolean
- Case “System.Guid”
- Return NullGuid
- Case Else
- ‘ Enumerations default to the first entry
- Dim pType As Type = objPropertyInfo.PropertyType
- If pType.BaseType.Equals(GetType(System.Enum)) Then
- Dim objEnumValues As System.Array = System.Enum.GetValues(pType)
- Array.Sort(objEnumValues)
- Return System.Enum.ToObject(pType, objEnumValues.GetValue(0))
- Else ‘ complex object
- Return Nothing
- End If
- 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 objField Is Nothing Then
- Return objDBNull
- ElseIf TypeOf objField Is Short Then
- If Convert.ToInt16(objField) = NullShort Then
- Return objDBNull
- End If
- ElseIf TypeOf objField Is Integer Then
- If Convert.ToInt32(objField) = NullInteger Then
- Return objDBNull
- End If
- ElseIf TypeOf objField Is Single Then
- If Convert.ToSingle(objField) = NullSingle Then
- Return objDBNull
- End If
- ElseIf TypeOf objField Is Double Then
- If Convert.ToDouble(objField) = NullDouble Then
- Return objDBNull
- End If
- ElseIf TypeOf objField Is Decimal Then
- If Convert.ToDecimal(objField) = NullDecimal Then
- Return objDBNull
- End If
- ElseIf TypeOf objField Is Date Then
- ‘ compare the Date part of the DateTime with the DatePart of the NullDate ( this avoids subtle time differences )
- If Convert.ToDateTime(objField).Date = NullDate.Date Then
- Return objDBNull
- End If
- ElseIf TypeOf objField Is String Then
- If objField Is Nothing Then
- Return objDBNull
- Else
- If objField.ToString = NullString Then
- Return objDBNull
- End If
- End If
- ElseIf TypeOf objField Is Boolean Then
- If Convert.ToBoolean(objField) = NullBoolean Then
- Return objDBNull
- End If
- ElseIf TypeOf objField Is Guid Then
- If CType(objField, System.Guid).Equals(NullGuid) Then
- Return objDBNull
- End If
- 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 Not objField Is Nothing Then
- If TypeOf objField Is Integer Then
- Return objField.Equals(NullInteger)
- ElseIf TypeOf objField Is Single Then
- Return objField.Equals(NullSingle)
- ElseIf TypeOf objField Is Double Then
- Return objField.Equals(NullDouble)
- ElseIf TypeOf objField Is Decimal Then
- Return objField.Equals(NullDecimal)
- ElseIf TypeOf objField Is Date Then
- Dim objDate As DateTime = CType(objField, DateTime)
- Return objDate.Date.Equals(NullDate.Date)
- ElseIf TypeOf objField Is String Then
- Return objField.Equals(NullString)
- ElseIf TypeOf objField Is Boolean Then
- Return objField.Equals(NullBoolean)
- ElseIf TypeOf objField Is Guid Then
- Return objField.Equals(NullGuid)
- Else ‘ complex object
- Return False
- End If
- Else
- Return True
- End If
- End Function
- End Class
행복한 고수되십시오.
WooGong ))*
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\