CSLA .NET

From Rockford Lhotka's Expert C# 2005 and VB 2005 Business Objects books

Welcome to CSLA .NET Sign in | Join | Help
in Search

Count Rule with AddNewCore

Last post 05-12-2008, 2:22 PM by sgraham. 2 replies.
Sort Posts: Previous Next
  •  05-12-2008, 1:06 PM 23519

    Count Rule with AddNewCore

    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?


    Thanks,

    Sean
  •  05-12-2008, 2:02 PM 23522 in reply to 23519

    Re: Count Rule with AddNewCore

    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

     

  •  05-12-2008, 2:22 PM 23524 in reply to 23522

    Re: Count Rule with AddNewCore

    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!


    Thanks,

    Sean
View as RSS news feed in XML

Please contact Magenic for your .NET consulting and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Powered by Community Server, by Telligent Systems