Possible request : BrokenRulesCollection

Possible request : BrokenRulesCollection

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


decius posted on Friday, January 09, 2009

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.

skagen00 replied on Friday, January 09, 2009

this.BrokenRulesCollection.GetFirstBrokenRule("MyProperty");  ... that work for you?

decius replied on Friday, January 09, 2009

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.

skagen00 replied on Friday, January 09, 2009

You understand that method gets the first broken rule that is in the collection that applies to that property, right? It could be the 5th in the list.

SonOfPirate replied on Saturday, January 10, 2009

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.

skagen00 replied on Saturday, January 10, 2009

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.

 

simon_may replied on Sunday, January 11, 2009

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

decius replied on Monday, January 12, 2009

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.

ajj3085 replied on Monday, January 12, 2009

Can't you case the BO to IDataErrorInfo and use the indexer?  That should give you a concatenation of all descriptions of broken rules.

simon_may replied on Monday, January 12, 2009

That will work for a single BO but not for any broken rules that further down the graph, child or grandchild BOs,  that may still be causng the BO to be invalid

Copyright (c) Marimer LLC