At the client/UI development, I find myself frequently coding foreach loops of an object's BrokenRulesCollection just to determine if the collection contains a specific property name.
It would be nice if there was a brokenRules.Contains("MyProperty") overload, but there's not.
How are other people handling this scenario? I've thought about just adding the overload to the framework myself, but I HATE tweaking unless necessary.
this
.BrokenRulesCollection.GetFirstBrokenRule("MyProperty"); ... that work for you?No unfortunately, I need to find out if the collection contains a property, and I can't be sure that it is the first broken rule in the collection.
GetFirstBrokenRule is great if I only care to get the first broken rule or simply need to know if there are any broken rules for a property. However, I have had cases where I want to list ALL of the broken rules for a given property. And it sounds like decius is doing something similar.
When I want to do this, I use LINQ to generate a "sublist" of broken rules by property that I can iterate on. If it is empty, then no broken rules exist for that property and I don't waste my time iterating. If it isn't empty, then I don't waste my time iterating over the full list checking to see if each entry corresponds to the chosen property because my sublist only contains broken rules for that property.
I appreciate that it only gives you the first broken rule for that property, but I think from his question that GetFirstBrokenRule serves its purpose in his case - I think he just had a slight misunderstanding that it doesn't apply to only BrokenRulesCollection[0].
e.g. "No unfortunately, I need to find out if the collection contains a property, and I can't be sure that it is the first broken rule in the collection."
That said, it seems that a method such as this might perhaps contain an index overload from with which to start with - that way, one can iterate over all broken rules that apply to that property.
Of course, as you mention, if that's the goal, LINQ is a solution now too.
In my own BusinessBase and BusinessBaseList classes (these inherit directly from the CSLA classes) I implement an IBrokenRuleSource interface which declares one Property __BrokenRules that returns a string array containing all brokent rules in the object graph. So a call the the Root BO __BrokentRules property walks through the graph collecting the broken rules.
Further all my forms Save button click events check validity of the BO and outputs the broken rules in a messagebox - all implemented in the Forms base class. Save loads of time when developing and testing new BOs
HTH
Regards
Simon May
Thanks for the insights. Yes, GetFirstBrokenRule returns the first rule for the given property, but i need all the broken rules for a particular property, sorry if I wasn't clear on that.
what i'v been doing is iterating with foreach to stuff the completed message into something like a StringBuilder. But that's a lot of unnecessary iterations I bet! I guess I could use the GetFirstBrokenRule and then check if it's null before iterating. That would atleast avoid unnessary iterations.
Perhaps what I really need is a GetBrokenRules("MyProperty") that returns an array of rules for the given property. I'll keep hunting, perhaps what I needs there and I just have never found it.
I like that LINQ concept though! I might start using that.
Copyright (c) Marimer LLC