CSLA .NET

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

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

Newbie Question - Validate that at least 1 child row exists

Last post 01-05-2009, 10:55 AM by ajj3085. 4 replies.
Sort Posts: Previous Next
  •  12-30-2008, 7:24 PM 29410

    Newbie Question - Validate that at least 1 child row exists

    Hello,

    Sorry if this the answer is painfully obvious, but I am pondering the best way for an Editable root object (with a BusinessListBase 'child' list property) to validate there is at least 1 entered/non-deleted child object on a Save. 

    Do I override BusinessBase.Save() and do my own checking, or is there better and/or more elegant way to handle this?

    Thanks in advance for any help.

    Rich.
  •  12-30-2008, 8:04 PM 29412 in reply to 29410

    Re: Newbie Question - Validate that at least 1 child row exists

    It would be helpful if you indicated why the pattern in the project tracker sample application does not work for you. The Project class demonstrates how a root object saves its children.

  •  12-30-2008, 9:01 PM 29416 in reply to 29410

    Re: Newbie Question - Validate that at least 1 child row exists

    I think it's best to use a validation rule so that your root object's IsValid flag and broken rules collection works as they should. A rule is usually applied to a property so your requirement of at least one child doesn't quite fit the normal pattern. I think at least one clean way to do it would be to create a boolean property of your root called something like "HasChildren" which returns the value of ChildList.Count > 0. Then you can create a simple rule that checks HasChildren and returns valid if HasChildren is true.

    The only issue I can think of is deciding when to actually check that rule. You probably need to catch the ListChanged event of the list and do a ValidationRules.CheckRules("HasChildren").

    I've done this sort of thing with a Windows forms UI and bound a checkbox of zero size (so it's invisible) in a convenient place on the form so that the data error icon appears when the rule is broken.

    Ross
  •  12-31-2008, 11:13 AM 29426 in reply to 29416

    Re: Newbie Question - Validate that at least 1 child row exists

    Thanks Ross, this is exactly what I was looking for...  Have a safe and happy new year!

    Rich.
  •  01-05-2009, 10:55 AM 29475 in reply to 29416

    Re: Newbie Question - Validate that at least 1 child row exists

    If it's Csla 3.5 or higher, you should be able to do this by override OnChildChanged.
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