need function for "are any in this subset of rules broken"?

need function for "are any in this subset of rules broken"?

Old forum URL: forums.lhotka.net/forums/t/7130.aspx


matt tag posted on Thursday, June 18, 2009

I have several complex objects that share some common traits:

ID (primary key in the database, Guid)
Name (string property)
Category (string property, acts as a grouping mechanism when viewing the names)
IsPublic (viewable by only you or everybody)

I wanted to use a common UI form to edit these properties for all of the object types.  Sounds like an Interface, correct?

Public Interface IPreflistName
    ReadOnly Property ListID() As Guid
    Property ListName() As String
    Property ListCategory() As String
    Property IsPublicList() As Boolean

    ReadOnly Property IsValid() As Boolean
    ReadOnly Property IsDirty() As Boolean
    Sub BeginEdit()
    Sub ApplyEdit()
    Sub CancelEdit()
End Interface


The only problem I have is the IsValid.  I don't actually want this interface to tie to the CSLA IsValid properly, because there may be other broken rules on the object that have nothing to do with the object's name/category.  What i need to do is rename this property to something like NameIsValid, and then within the classes, I need to be able to say  "if this rule is broken or that rule is broken, return false".

Can I pick certain rules out of my class in this way?

JoeFallon1 replied on Thursday, June 18, 2009

I needed to do something like this so I added this method to a utility class which all of my BOs can call:

===============================
Public Shared Sub ClearBrokenRules(ByVal ruleCollection As Csla.Validation.BrokenRulesCollection)
For i As Integer = ruleCollection.Count - 1 To 0 Step -1
Dim brokenRule As Csla.Validation.BrokenRule = ruleCollection.Item(i)
ruleCollection.Remove(brokenRule)
Next
End Sub

===============================
It required adding a change to CSLA though:

//Change to Validation.BrokenRulesCollection class:
//JF 6/28/07 - need a Public way to clear broken rules other than by calling CheckRules.
public void Remove(BrokenRule rule)
{
// we loop through using a numeric counter because removing items in a foreach isn't reliable
IsReadOnly = false;
for (int index = 0; index {
if (this[index].RuleName == rule.RuleName)
{
DecrementCount(this[index]);
RemoveAt(index);
break;
}
}
IsReadOnly = true;
}

===============================
Then in a BO that contains other BOs I can use code like this:

Public ReadOnly Property IsSubsetValid() As Boolean
Get
'check all rules
ValidationRules.CheckRules()
Return GetSubsetOfBrokenRules
End Property

Public Function GetSubsetOfBrokenRules() As String
'remove broken rules from other objects in the graph
ClearUnrelatedBrokenRules()
Return Me.GetAllBrokenRulesString
End Function

'clear rules from a contained BO and its children
Private Sub ClearUnrelatedBrokenRules()
Util.ClearBrokenRules(mContainedBO.BrokenRulesCollection)

For Each obj As SomeBO In mContainedBO.Coll
Util.ClearBrokenRules(obj.BrokenRulesCollection)
Next
End Sub
===============================

Joe

Copyright (c) Marimer LLC