Count Rule with AddNewCore

Count Rule with AddNewCore

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


sgraham posted on Monday, May 12, 2008

I have a parent object that contains a collection of children.

Parent -> ChildCollection - > Child

I have a business rule that the parent must have at least two children (rule is attached to "Children" attribute).  In order to track counts and changes, the parent has AddChild and RemoveChild methods which call the PropertyHasChanged("Children").

I want to allow grid additions (using AddNewCore and AllowNew = true).  But, this model allows the child collection to do it's own add so the parent isn't aware of the change.

What can I do?

Is there a different/better way to implement the rule?

Is there a way for the child to nofity the parent?

Thoughts?

JoeFallon1 replied on Monday, May 12, 2008

Sean,

2 ideas.

1. Better way to implement the rule? I don't know about better - but here is another way.

I sometimes override IsValid and before calling MyBase.IsValid I will call Validationrules.CheckRules("Children").

I usually only do this with rules which are not attached to real properties and I just use a fake property name. I only want them to run after all the property sets have occurred - the most common example in my web forms is the use of checkboxes - I have a rule that at least one of them must be checked - why bother checking the rule 10 times when I only need it to run once after they are all evaluated? This may not work well in a Winforms environment though. Depends on how early you want the rule evaluated. My Save button always calls IsValid before attempting the save so I know if the rule is broken or not.

 

2. Is there a way for the child to nofity the parent?

Yes. I do not do it but there have been many posts on the topic in the past. In fact the issue has been such a problem that Rocky addressed it in 3.5 so that it happens automatically. (I think.) He has a whole section of code on managing child objects automatically. So you can either dig through some old threads or wait for Andy to post the solution in his response. <g>

Joe

 

sgraham replied on Monday, May 12, 2008

I'm still on 2.x so option 2 isn't available to be at this point

But, option 1 worked great!  Thanks for the help!

Copyright (c) Marimer LLC