Skip to content

Commit

Permalink
Improved error message when a FieldMapping cannot be set or read. Closes
Browse files Browse the repository at this point in the history
 #54.
  • Loading branch information
hisystems committed May 5, 2012
1 parent 87836cb commit 99389fc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
24 changes: 12 additions & 12 deletions Database/DatabaseObjectUsingAttributesHelper.vb
Expand Up @@ -81,10 +81,10 @@ Public Module DatabaseObjectUsingAttributesHelper
If Not objAttributes Is Nothing Then
For Each objFieldMappingObjectHook As FieldMappingObjectHookAttribute In objAttributes
If objField.FieldType.IsValueType Then
Throw New Exceptions.DatabaseObjectsException("Field " & objField.Name & " marked with FieldMappingObjectHook attribute on value type - must be a class type")
Throw New Exceptions.DatabaseObjectsException("Field " & objField.FullName & " marked with FieldMappingObjectHook attribute on value type - must be a class type")
Else
objFieldObject = objField.GetValue(objObject)
If objFieldObject Is Nothing Then Throw New Exceptions.DatabaseObjectsException("Field " & objField.Name & " marked with " & GetType(FieldMappingObjectHookAttribute).Name & " is Nothing")
If objFieldObject Is Nothing Then Throw New Exceptions.DatabaseObjectsException("Field " & objField.FullName & " marked with " & GetType(FieldMappingObjectHookAttribute).Name & " is Nothing")
objFieldValues.Add(SaveFieldsForBaseTypes(objFieldObject, objFieldObject.GetType))
End If
Next
Expand All @@ -98,10 +98,10 @@ Public Module DatabaseObjectUsingAttributesHelper
For Each objFieldMappingObjectHook As FieldMappingObjectHookAttribute In objAttributes
If objProperty.CanRead Then
If objProperty.PropertyType.IsValueType Then
Throw New Exceptions.DatabaseObjectsException("Property " & objProperty.Name & " marked with FieldMappingObjectHook attribute on value type - must be a class type")
Throw New Exceptions.DatabaseObjectsException("Property " & objProperty.FullName & " marked with FieldMappingObjectHook attribute on value type - must be a class type")
Else
objFieldObject = objProperty.GetValue(objObject, Nothing)
If objFieldObject Is Nothing Then Throw New Exceptions.DatabaseObjectsException("Property " & objProperty.Name & " marked with " & GetType(FieldMappingObjectHookAttribute).Name & " is Nothing")
If objFieldObject Is Nothing Then Throw New Exceptions.DatabaseObjectsException("Property " & objProperty.FullName & " marked with " & GetType(FieldMappingObjectHookAttribute).Name & " is Nothing")
objFieldValues.Add(SaveFieldsForBaseTypes(objFieldObject, objFieldObject.GetType))
End If
End If
Expand Down Expand Up @@ -140,7 +140,7 @@ Public Module DatabaseObjectUsingAttributesHelper
objFieldValues.Add(objFieldMapping.FieldName, objField.GetValue(objObject))
End If
Catch ex As Exception
Throw New Exceptions.DatabaseObjectsException("Field '" & objType.Name & "." & objField.Name & "' could not be read.", ex)
Throw New Exceptions.DatabaseObjectsException("Field '" & objField.FullName & "' could not be read.", ex)
End Try
Next
End If
Expand All @@ -160,7 +160,7 @@ Public Module DatabaseObjectUsingAttributesHelper
objFieldValues.Add(objFieldMapping.FieldName, objProperty.GetValue(objObject, Nothing))
End If
Catch ex As Exception
Throw New Exceptions.DatabaseObjectsException("Property '" & objType.Name & "." & objProperty.Name & "' could not be read.", ex)
Throw New Exceptions.DatabaseObjectsException("Property '" & objProperty.FullName & "' could not be read.", ex)
End Try
End If
Next
Expand Down Expand Up @@ -235,10 +235,10 @@ Public Module DatabaseObjectUsingAttributesHelper
If Not objAttributes Is Nothing Then
For Each objFieldMappingObjectHook As FieldMappingObjectHookAttribute In objAttributes
If objField.FieldType.IsValueType Then
Throw New Exceptions.DatabaseObjectsException("Field " & objField.Name & " marked with FieldMappingObjectHook attribute on value type - must be a class type")
Throw New Exceptions.DatabaseObjectsException("Field " & objField.FullName & " marked with FieldMappingObjectHook attribute on value type - must be a class type")
Else
objFieldObject = objField.GetValue(objObject)
If objFieldObject Is Nothing Then Throw New Exceptions.DatabaseObjectsException("Field " & objField.Name & " marked with " & GetType(FieldMappingObjectHookAttribute).Name & " is Nothing")
If objFieldObject Is Nothing Then Throw New Exceptions.DatabaseObjectsException("Field " & objField.FullName & " marked with " & GetType(FieldMappingObjectHookAttribute).Name & " is Nothing")
LoadFieldsForBaseTypes(objFieldObject, objFieldObject.GetType, objFields)
End If
Next
Expand All @@ -252,10 +252,10 @@ Public Module DatabaseObjectUsingAttributesHelper
For Each objFieldMappingObjectHook As FieldMappingObjectHookAttribute In objAttributes
If objProperty.CanRead Then
If objProperty.PropertyType.IsValueType Then
Throw New Exceptions.DatabaseObjectsException("Property " & objProperty.Name & " marked with FieldMappingObjectHook attribute on value type - must be a class type")
Throw New Exceptions.DatabaseObjectsException("Property " & objProperty.FullName & " marked with FieldMappingObjectHook attribute on value type - must be a class type")
Else
objFieldObject = objProperty.GetValue(objObject, Nothing)
If objFieldObject Is Nothing Then Throw New Exceptions.DatabaseObjectsException("Property " & objProperty.Name & " marked with " & GetType(FieldMappingObjectHookAttribute).Name & " is Nothing")
If objFieldObject Is Nothing Then Throw New Exceptions.DatabaseObjectsException("Property " & objProperty.FullName & " marked with " & GetType(FieldMappingObjectHookAttribute).Name & " is Nothing")
LoadFieldsForBaseTypes(objFieldObject, objFieldObject.GetType, objFields)
End If
End If
Expand Down Expand Up @@ -294,7 +294,7 @@ Public Module DatabaseObjectUsingAttributesHelper
objField.SetValue(objObject, objFields(objFieldMapping.FieldName).Value)
End If
Catch ex As Exception
Throw New Exceptions.DatabaseObjectsException("Field '" & objField.Name & "' could not be set.", ex)
Throw New Exceptions.DatabaseObjectsException("Field '" & objField.FullName & "' could not be set.", ex)
End Try
End If
Next
Expand All @@ -320,7 +320,7 @@ Public Module DatabaseObjectUsingAttributesHelper
objProperty.SetValue(objObject, objFields(objFieldMapping.FieldName).Value, Nothing)
End If
Catch ex As Exception
Throw New Exceptions.DatabaseObjectsException("Property '" & objProperty.Name & "' could not be set.", ex)
Throw New Exceptions.DatabaseObjectsException("Property '" & objProperty.FullName & "' could not be set.", ex)
End Try
End If
End If
Expand Down
1 change: 1 addition & 0 deletions DatabaseObjects.vbproj
Expand Up @@ -154,6 +154,7 @@
<Compile Include="Database\DatabaseObjectsEnumerable.vb">
<SubType>Code</SubType>
</Compile>
<Compile Include="Misc\SystemReflectionExtensions.vb" />
<Compile Include="Obsolete\DatabaseObjectUsingAttributes.vb">
<SubType>Code</SubType>
</Compile>
Expand Down
27 changes: 27 additions & 0 deletions Misc/SystemReflectionExtensions.vb
@@ -0,0 +1,27 @@
Imports System.Runtime.CompilerServices
Imports System.IO

Friend Module SystemReflectionExtensions

''' <summary>
''' Returns the full class name and field name.
''' </summary>
<Extension()>
Public Function FullName(ByVal field As System.Reflection.FieldInfo) As String

Return field.DeclaringType.FullName & System.Type.Delimiter & field.Name

End Function

''' <summary>
''' Returns the full class name and property name.
''' </summary>
<Extension()>
Public Function FullName(ByVal [property] As System.Reflection.PropertyInfo) As String

Return [property].DeclaringType.FullName & System.Type.Delimiter & [property].Name

End Function

End Module

0 comments on commit 99389fc

Please sign in to comment.