You usually have to write friend/internal methods for your
parent/child classes to expose them.
If you want more generic way, here is a class I wrote that
generates one error message for entire graph. Feel free to modify it to
suit your needs:
Imports System.ComponentModel
Public Class CSLABusinessObjectErrorTextExaminer
Private Sub New()
End Sub
''' <summary>
''' Get text for errors if object is invalid
''' </summary>
''' <returns>Error text for invalid
object</returns>
''' <remarks></remarks>
Public Shared Function
GetErrorInformation(ByVal target As Object) As String
Dim inspectedObjects
As New Collections.Generic.List(Of String)
Return
GetErrorInformation(target, inspectedObjects)
End Function
''' <summary>
''' Get text for errors for one object based
on IDataErrorInfo interface
''' </summary>
''' <param
name="target">Object to get error text for</param>
''' <param
name="inspectedObjects">List of hash codes of objects that are
inspected.
''' This is necessary to avoid infinite
recursion.
''' </param>
''' <returns>Text for errors for one
object based on IDataErrorInfo interface</returns>
''' <remarks></remarks>
Private Shared Function GetErrorInformation(ByVal
target As Object, ByVal inspectedObjects As Collections.Generic.List(Of
String)) As String
Dim returnValue As
New Text.StringBuilder()
Dim targetID As
String = String.Empty
If target IsNot
Nothing Then
targetID = target.GetType.ToString & ":" &
target.GetHashCode().ToString
End If
If target IsNot
Nothing AndAlso Not inspectedObjects.Contains(targetID) Then
inspectedObjects.Add(targetID)
If TypeOf target Is IBindingList Then
' check error messages for each row in the list
For oneItem As Integer = 0 To CType(target, IBindingList).Count - 1
If TypeOf CType(target, IBindingList)(oneItem) Is Csla.Core.BusinessBase Then
Dim
itemError As String = GetErrorInformation(CType(target, IBindingList)(oneItem),
inspectedObjects)
' if we do not have this message already, add it
If itemError.Length > 0 AndAlso Not returnValue.ToString.Contains(itemError)
Then
returnValue.Append(itemError)
returnValue.Append(Environment.NewLine)
End If
End If
Next
Else
If TypeOf target Is Csla.Core.BusinessBase Then
' run through broken rules collection for this object
For Each oneBrokenRule As Csla.Validation.BrokenRule In CType(target,
Csla.Core.BusinessBase).BrokenRulesCollection
' if we do not have this message already, add it
If Not returnValue.ToString.Contains(oneBrokenRule.Description) Then
returnValue.Append(oneBrokenRule.Description)
returnValue.Append(Environment.NewLine)
End If
Next
' get list of properties for this object
Dim properties() As System.Reflection.PropertyInfo =
target.GetType.GetProperties()
For Each oneProperty As System.Reflection.PropertyInfo In properties
' get object that sits on this property
If Not oneProperty.PropertyType.IsPrimitive AndAlso
oneProperty.GetIndexParameters().Length = 0 Then
Dim propTarget As Object = oneProperty.GetValue(target, Nothing)
' call this procedure resursively to get error for property based object
Dim propErrorText As String = GetErrorInformation(propTarget, inspectedObjects)
' if we do not have this message already, add it
If propErrorText.Length > 0 AndAlso Not returnValue.ToString.Contains(propErrorText)
Then
returnValue.Append(propErrorText)
returnValue.Append(Environment.NewLine)
End If
End If
Next
End If
End If
End If
' strip out duplicate
carriage returns before returning the value.
Return
returnValue.ToString.Replace(Environment.NewLine & Environment.NewLine,
Environment.NewLine)
End Function
End Class
Sergey Barskiy
Senior Consultant
office: 678.405.0687 |
mobile: 404.388.1899
Magenic ®
Microsoft Worldwide Partner of the Year | Custom
Development Solutions, Technical Innovation
From: Lalit
[mailto:cslanet@lhotka.net]
Sent: Wednesday, May 21, 2008 8:07 AM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] How to access broken rules of base class
And how can we display broken rules of child with its parent's broken rules?