Hi,
The first GetAllBroken rules would check if object was valid or not. Unfortunately this would miss objects with only warning or info.
I have a never version at work that will also accept a bool parameter to include all error levels. I can post the updated code later today.
It is not in CSLA Trunk (yet).
Hi,
Try this code, when used with errorLevelOnly set to false you will get all objects with broken rules on any level. :
#region Get All Broken Rules (tree) /// <summary> /// Gets all broken rules with Error level in object graph. /// </summary> /// <param name="root">The root.</param> /// <returns>BrukenRulesTree list</returns> public static BrokenRulesTree GetAllBrokenRules(object root) { return GetAllBrokenRules(root, true); } /// <summary> /// Gets all broken rules. /// </summary> /// <param name="root">The root.</param> /// <returns></returns> public static BrokenRulesTree GetAllBrokenRules(object root, bool errorsOnly) { var list = new BrokenRulesTree(); long counter = 1; long childBrokenRuleCount = 0; AddNodeToBrukenRules(ref list, ref counter, null, root, errorsOnly, ref childBrokenRuleCount); return list; } private static void AddNodeToBrukenRules(ref BrokenRulesTree list, ref long counter, object parentKey, object obj, bool errorsOnly, ref long childBrokenRuleCount) { // is this a single editable object if (obj is Csla.Core.BusinessBase) { var nodeKey = counter++; var bo = (Csla.Core.BusinessBase)obj; long myChildBrokenRuleCount = bo.BrokenRulesCollection.Count; var node = new BrokenRulesNode() { Parent = parentKey, Node = nodeKey, BrokenRules = bo.BrokenRulesCollection, Object = obj }; list.Add(node); // get managed child properties foreach (var child in ((IManageProperties)bo).GetChildren()) { AddNodeToBrukenRules(ref list, ref counter, nodeKey, child, errorsOnly, ref myChildBrokenRuleCount); } // remove node if it has no child with broken rules. if (!errorsOnly && myChildBrokenRuleCount == 0) { list.Remove(node); } if (errorsOnly && bo.IsValid) { list.Remove(node); } childBrokenRuleCount += myChildBrokenRuleCount; } // or a list of EditableObject (both BindingList and ObservableCollection) else if (obj is IEditableCollection) { var nodeKey = counter++; var isValid = ((ITrackStatus)obj).IsValid; var node = new BrokenRulesNode() { Parent = parentKey, Node = nodeKey, Object = obj }; long myChildBrokenRuleCount = 0; list.Add(node); foreach (var child in (IEnumerable)obj) { AddNodeToBrukenRules(ref list, ref counter, nodeKey, child, errorsOnly, ref myChildBrokenRuleCount); } // remove node if it has no child with broken rules. if (!errorsOnly && myChildBrokenRuleCount == 0) { list.Remove(node); } if (errorsOnly && isValid) { list.Remove(node); } childBrokenRuleCount += myChildBrokenRuleCount; } return; } #endregion
Works great, thanks!
Hope to see this in a future version of CSLA.
Copyright (c) Marimer LLC