CSLA .NET

From Rockford Lhotka's Expert C# 2005 and VB 2005 Business Objects books

Welcome to CSLA .NET Sign in | Join | Help
in Search

How to access broken rules of base class

Last post 05-21-2008, 4:40 PM by JohnB. 4 replies.
Sort Posts: Previous Next
  •  05-20-2008, 3:29 AM 23682

    How to access broken rules of base class

    I have a scenario in CSLA 2.1, where i inhereted a class into another one. Both are having their validation rules. Now if i want broken rules of base class as well in inhereted class's BrokenRuleCollection, is there any functionality for same? How can i get it all broken rules by just accessing inhereted one's broken rules?

  •  05-20-2008, 8:57 AM 23694 in reply to 23682

    Re: How to access broken rules of base class

    The derived class already has all the broken rules from the base class. You do not have to do anything.

    Me.BrokenRulesCollection.ToString

    Joe

     

  •  05-21-2008, 5:51 AM 23715 in reply to 23694

    Re: How to access broken rules of base class

    And how can we display broken rules of child with its parent's broken rules?

  •  05-21-2008, 7:56 AM 23717 in reply to 23715

    RE: How to access broken rules of base class

    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?



  •  05-21-2008, 4:40 PM 23728 in reply to 23715

    Re: How to access broken rules of base class

    Lalit,

    I posted an example some time ago to do this. It should be enough to get you started.

    John
View as RSS news feed in XML

Please contact Magenic for your .NET consulting and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Powered by Community Server, by Telligent Systems