1: Private Shared Sub HydrateObject(ByVal objObject As Object, ByVal dr As IDataReader)
2: Dim objPropertyInfo As PropertyInfo = Nothing
3: Dim objPropertyType As Type = Nothing
4: Dim objDataValue As Object
5: Dim objDataType As Type
6: Dim intIndex As Integer
7:
8: ' get cached object mapping for type
9: Dim objMappingInfo As ObjectMappingInfo = GetObjectMapping(objObject.GetType)
10:
11: ' fill object with values from datareader
12: For intIndex = 0 To dr.FieldCount - 1
13: 'If the Column matches a Property in the Object Map's PropertyInfo Dictionary
14: If objMappingInfo.Properties.TryGetValue(dr.GetName(intIndex).ToUpperInvariant,
objPropertyInfo) Then
15: 'Get its type
16: objPropertyType = objPropertyInfo.PropertyType
17:
18: 'If property can be set
19: If objPropertyInfo.CanWrite Then
20: 'Get the Data Value from the data reader
21: objDataValue = dr.GetValue(intIndex)
22:
23: 'Get the Data Value's type
24: objDataType = objDataValue.GetType
25:
26: If IsDBNull(objDataValue) Then
27: ' set property value to Null
28: objPropertyInfo.SetValue(objObject, Null.SetNull(objPropertyInfo), Nothing)
29: ElseIf objPropertyType.Equals(objDataType) Then
30: 'Property and data objects are the same type
31: objPropertyInfo.SetValue(objObject, objDataValue, Nothing)
32: Else
33: ' business object info class member data type does not match
34: ' datareader member data type
35: Try
36: 'need to handle enumeration conversions differently than other
37: 'base(types)
38: If objPropertyType.BaseType.Equals(GetType(System.Enum)) Then
39: ' check if value is numeric and if not convert to integer
40: ' ( supports databases like Oracle )
41: If IsNumeric(objDataValue) Then
42: objPropertyInfo.SetValue(objObject, System.Enum.ToObject(
objPropertyType, Convert.ToInt32(objDataValue)), Nothing)
43: Else
44: objPropertyInfo.SetValue(objObject, System.Enum.ToObject(
objPropertyType, objDataValue), Nothing)
45: End If
46: ElseIf objPropertyType.FullName.Equals("System.Guid") Then
47: ' guid is not a datatype common across all databases
48: ' ( ie. Oracle )
49: objPropertyInfo.SetValue(objObject, Convert.ChangeType(
New Guid(objDataValue.ToString()), objPropertyType), Nothing)
50: ElseIf objPropertyType.FullName.Equals("System.Version") Then
51: objPropertyInfo.SetValue(objObject,
New Version(objDataValue.ToString()), Nothing)
52: Else
53: ' try explicit conversion
54: objPropertyInfo.SetValue(objObject, objDataValue, Nothing)
55: End If
56: Catch
57: objPropertyInfo.SetValue(objObject, Convert.ChangeType(objDataValue,
objPropertyType), Nothing)
58: End Try
59: End If
60: End If
61: End If
62: Next
63: End Sub