[VB.NET] Null Class as Utility

Null class as Utility

Null Class as Utility

Utility 로 쓰면 어떨까 해서 올립니다.

1. Utility 로 사용할 Null Class

첫 소스는 codeproject 에서 가져왔는데요
조금 조금씩 수정했습니다.
코드를 보시면 이걸 왜 이렇게 했을까… 라는 생각을 갖게 하는 부분들이 있습니다.  ^^

필요하신 부분 있으시면 수정해서 사용하시면 좋을 듯합니다.

이런 코드도 있더라 정도로 생각해 두시면… ^^

이런식으로 자신만의 함수들을 따로 만들어 놓으시면
자신만의 프래임웍 유틸리티를 만들수 있겠죠? ^^

2. Code

Code Snippet
  1. Imports System
  2. Imports System.Reflection
  3.  
  4. Public Class Null
  5.     ‘ define application encoded null values
  6.     Public Shared ReadOnly Property NullShort() As Short
  7.         Get
  8.             Return -1
  9.         End Get
  10.     End Property
  11.     Public Shared ReadOnly Property NullInteger() As Integer
  12.         Get
  13.             Return -1
  14.         End Get
  15.     End Property
  16.     Public Shared ReadOnly Property NullSingle() As Single
  17.         Get
  18.             Return Single.MinValue
  19.         End Get
  20.     End Property
  21.     Public Shared ReadOnly Property NullDouble() As Double
  22.         Get
  23.             Return Double.MinValue
  24.         End Get
  25.     End Property
  26.     Public Shared ReadOnly Property NullDecimal() As Decimal
  27.         Get
  28.             Return Decimal.MinValue
  29.         End Get
  30.     End Property
  31.     Public Shared ReadOnly Property NullDate() As Date
  32.         Get
  33.             Return Date.MinValue
  34.         End Get
  35.     End Property
  36.     Public Shared ReadOnly Property NullString() As String
  37.         Get
  38.             Return “”
  39.         End Get
  40.     End Property
  41.     Public Shared ReadOnly Property NullBoolean() As Boolean
  42.         Get
  43.             Return False
  44.         End Get
  45.     End Property
  46.     Public Shared ReadOnly Property NullGuid() As Guid
  47.         Get
  48.             Return Guid.Empty
  49.         End Get
  50.     End Property
  51.     ‘ sets a field to an application encoded null value ( used in BLL layer )
  52.     Public Shared Function SetNull(ByVal objValue As Object, ByVal objField As Object) As Object
  53.         If IsDBNull(objValue) Then
  54.             If TypeOf objField Is Short Then
  55.                 Return NullShort
  56.             ElseIf TypeOf objField Is Integer Then
  57.                 Return NullInteger
  58.             ElseIf TypeOf objField Is Single Then
  59.                 Return NullSingle
  60.             ElseIf TypeOf objField Is Double Then
  61.                 Return NullDouble
  62.             ElseIf TypeOf objField Is Decimal Then
  63.                 Return NullDecimal
  64.             ElseIf TypeOf objField Is Date Then
  65.                 Return NullDate
  66.             ElseIf TypeOf objField Is String Then
  67.                 Return NullString
  68.             ElseIf TypeOf objField Is Boolean Then
  69.                 Return NullBoolean
  70.             ElseIf TypeOf objField Is Guid Then
  71.                 Return NullGuid
  72.             Else ‘ complex object
  73.                 Return Nothing
  74.             End If
  75.         Else ‘ return value
  76.             Return objValue
  77.         End If
  78.     End Function
  79.     ‘ sets a field to an application encoded null value ( used in BLL layer )
  80.     Public Shared Function SetNull(ByVal objPropertyInfo As PropertyInfo) As Object
  81.         Select Case objPropertyInfo.PropertyType.ToString
  82.             Case “System.Int16”
  83.                 Return NullShort
  84.             Case “System.Int32”, “System.Int64”
  85.                 Return NullInteger
  86.             Case “System.Single”
  87.                 Return NullSingle
  88.             Case “System.Double”
  89.                 Return NullDouble
  90.             Case “System.Decimal”
  91.                 Return NullDecimal
  92.             Case “System.DateTime”
  93.                 Return NullDate
  94.             Case “System.String”, “System.Char”
  95.                 Return NullString
  96.             Case “System.Boolean”
  97.                 Return NullBoolean
  98.             Case “System.Guid”
  99.                 Return NullGuid
  100.             Case Else
  101.                 ‘ Enumerations default to the first entry
  102.                 Dim pType As Type = objPropertyInfo.PropertyType
  103.                 If pType.BaseType.Equals(GetType(System.Enum)) Then
  104.                     Dim objEnumValues As System.Array = System.Enum.GetValues(pType)
  105.                     Array.Sort(objEnumValues)
  106.                     Return System.Enum.ToObject(pType, objEnumValues.GetValue(0))
  107.                 Else ‘ complex object
  108.                     Return Nothing
  109.                 End If
  110.         End Select
  111.     End Function
  112.     ‘ convert an application encoded null value to a database null value ( used in DAL )
  113.     Public Shared Function GetNull(ByVal objField As Object, ByVal objDBNull As Object) As Object
  114.         GetNull = objField
  115.         If objField Is Nothing Then
  116.             Return objDBNull
  117.         ElseIf TypeOf objField Is Short Then
  118.             If Convert.ToInt16(objField) = NullShort Then
  119.                 Return objDBNull
  120.             End If
  121.         ElseIf TypeOf objField Is Integer Then
  122.             If Convert.ToInt32(objField) = NullInteger Then
  123.                 Return objDBNull
  124.             End If
  125.         ElseIf TypeOf objField Is Single Then
  126.             If Convert.ToSingle(objField) = NullSingle Then
  127.                 Return objDBNull
  128.             End If
  129.         ElseIf TypeOf objField Is Double Then
  130.             If Convert.ToDouble(objField) = NullDouble Then
  131.                 Return objDBNull
  132.             End If
  133.         ElseIf TypeOf objField Is Decimal Then
  134.             If Convert.ToDecimal(objField) = NullDecimal Then
  135.                 Return objDBNull
  136.             End If
  137.         ElseIf TypeOf objField Is Date Then
  138.             ‘ compare the Date part of the DateTime with the DatePart of the NullDate ( this avoids subtle time differences )
  139.             If Convert.ToDateTime(objField).Date = NullDate.Date Then
  140.                 Return objDBNull
  141.             End If
  142.         ElseIf TypeOf objField Is String Then
  143.             If objField Is Nothing Then
  144.                 Return objDBNull
  145.             Else
  146.                 If objField.ToString = NullString Then
  147.                     Return objDBNull
  148.                 End If
  149.             End If
  150.         ElseIf TypeOf objField Is Boolean Then
  151.             If Convert.ToBoolean(objField) = NullBoolean Then
  152.                 Return objDBNull
  153.             End If
  154.         ElseIf TypeOf objField Is Guid Then
  155.             If CType(objField, System.Guid).Equals(NullGuid) Then
  156.                 Return objDBNull
  157.             End If
  158.         End If
  159.     End Function
  160.     ‘ checks if a field contains an application encoded null value
  161.     Public Shared Function IsNull(ByVal objField As Object) As Boolean
  162.         If Not objField Is Nothing Then
  163.             If TypeOf objField Is Integer Then
  164.                 Return objField.Equals(NullInteger)
  165.             ElseIf TypeOf objField Is Single Then
  166.                 Return objField.Equals(NullSingle)
  167.             ElseIf TypeOf objField Is Double Then
  168.                 Return objField.Equals(NullDouble)
  169.             ElseIf TypeOf objField Is Decimal Then
  170.                 Return objField.Equals(NullDecimal)
  171.             ElseIf TypeOf objField Is Date Then
  172.                 Dim objDate As DateTime = CType(objField, DateTime)
  173.                 Return objDate.Date.Equals(NullDate.Date)
  174.             ElseIf TypeOf objField Is String Then
  175.                 Return objField.Equals(NullString)
  176.             ElseIf TypeOf objField Is Boolean Then
  177.                 Return objField.Equals(NullBoolean)
  178.             ElseIf TypeOf objField Is Guid Then
  179.                 Return objField.Equals(NullGuid)
  180.             Else ‘ complex object
  181.                 Return False
  182.             End If
  183.         Else
  184.             Return True
  185.         End If
  186.     End Function
  187. End Class

 

행복한 고수되십시오.

WooGong ))*
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\