Exposing rules of child to the parent.

Exposing rules of child to the parent.

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


Santa posted on Thursday, July 26, 2007

Hi

I have read through many posts on this forum on the subject and am still unclear on the best way to do this.

Basically I have a parent object A and Child B.  B is a list of items say C.

I have created rules for A and C and want to display errors on my UI if A is not valid ( A is invalid if any of C is invalid).

After testing for A is valid I want to loop through the BrokenRulesCollection and display the appropriate errors.  But I dont want to have to go through the broken rule collection for each of the children.  So my question is how store brokenrulecollection for all of my child objects inside A.

Thanks

Santa

triplea replied on Thursday, July 26, 2007

There was a post somewhere were Rocky was explaining why he he did not return the broken rules of children (can't find the post but it did exist :-) ).
But the questions is why don't you want to loop through all the broken rules of your children? Your object A knows all its children (B, <other children> etc) so you can just override the BrokenRules property or you can provide a public method that returns a string with the description of all your errors (including these of the children) to be displayed on your UI.
If you do that on object A, you only do so once (if you want to be fancy you can have a base class and with reflection check for broken rules on all your children but I can imagine that complicated) and in 1 location so that's not so bad...

Santa replied on Thursday, July 26, 2007

hmmm, thanks, I think I read Rocky's post.

I dont really want to return the broken rules as a string.  I want to return the BrokenRuleCollection itself as my UI template is designed around this.

Rocky mentions to subclass BusinessBase to try and add this functionality.  Now some of principles of OO are still a bit above me so this might be a load of rubbish, but here is what I was thinking.

I could subclass BusinessBase and in here use the singleton pattern to create a BrokenRuleCollection and have all my objects add rules to this collection.  The problem is that the BrokenRuleCollection class cannot be instanciated because it is internal.  So would I have to create my own BrokenRuleCollection which inherits from the Csla BrokenRuleCollection but has a public constructor?  Any help appreciated.

Maybe I should just settle for returning a string I've been looking at this for too long, but I think this will create other issues later on.

triplea replied on Thursday, July 26, 2007

I think Joe's advice is sound but since I am also not fluent with some OO principles just a piece of advice. Extedning BusinessBase, though very important, is not that trivial and if not done properly (I am still paying the price for old design mistakes and trying to get them right) can lead to the need to refactor at some stage. It might be worth you reading the relevant posts about extending BusinessBase before embarking on your venture. They will be useful for your case but also prepare you for other issues you might encounter :-)

JoeFallon1 replied on Thursday, July 26, 2007

I use the base class method to list out all the broken rules of a root BO and all of its contained BOs. I think I have posted it before. It was based on the 1.x implementation of AllRules.vb. I think I might have called it GetAllBrokenRulesString or something like that (if you want to look for it.)

Your Root BO can get the list of broken rules from its children by looping over each of them and getting the string. Your requirement for NOT looping is artificial and you should forget it.

There is a small framework problem though.

Rocky's IsValid stops evaluating the broken rules for a collection the moment it finds a problem.

So if you have 10 items in the list and the 1st item is invalid you won't know the real state of the next 9 items if you use the framework. I sometimes add calls to CheckRules in my child BOs IsValid method so that a final check is done before determing the list of broken rules. (e.g. a user could have changed a value to unbreak a general rule.)

In my base class that inherits from the framework I override IsValid like this:

'JF 9/14/2006 - override CSLA2 code to look like this - no short circuiting involved here.
'run through all the child objects and if any are invalid then the collection is invalid
'call IsValid on all children so that when we examine Broken Rules we get the right set of values.
'Rocky stopped looping after he found the first invalid child.

Public Overrides ReadOnly Property IsValid() As Boolean 
  Get
   
Dim result As Boolean = True
   
For Each child As C In Me
     
If Not child.IsValid Then
       
result = False
     
End If
   
Next
   
Return result
 
End Get
End Property

Joe

 

Copyright (c) Marimer LLC