Validation of children in a collection object

Validation of children in a collection object

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


razorkai posted on Friday, August 18, 2006

Hi guys.

I have seen similar posts to this but no definitive answer.  Say you have a collection that contains child objects and you add several children before calling save.  What if one of those children violates a business rule?  You get an "Object is not valid and cannot be saved" exception but how can you tell the user why the object is not valid?  The collection will have an empty BrokenRulesCollection and it seems the only way to know what the broken rule is and indeed which object is breaking it is to use the debugger - obviously not possible on a user's pc. 

How have other people soleved this problem?  I tried to work out a way to look at the object's properties, determine which were business objects and check their BrokenRulesCollection properties too but it got really messy, especially when children had child collections themselves!

Any help or advise with this would be great.  This is more of a problem in the web, as I can use an errorprovider and datagridview with a windows form.

John.

Brian Criswell replied on Friday, August 18, 2006

The way I like the best so far is to pop up a dialog that has a tree on it.  The tree displays the broken rules for the parent and all of its children.

RangerGuy replied on Friday, August 18, 2006

For the web ui... could you loop thru all the child collections checking for invalid children and grab the broken rules for each child?

I haven't touched validation yet so it's only a guess :)

razorkai replied on Friday, August 18, 2006

This is a reply to both Brian and RangerGuy.

Yes you can loop thru an object and display the child broken rules in a treeview or otherwise, but the key here is that you would have to code this functionality for every collection object i.e. I have not found a way to generically do this for all objects and show broken rules in a single dialog box. That is the real problem!

 

Brian Criswell replied on Friday, August 18, 2006

You can use reflection to iterate over the properties and look for properties that implement IDataErrorInfo or IList.  You can walk down these properties to the children, grandchildren, etc. and build the tree.

razorkai replied on Saturday, August 19, 2006

Brian

That is what I tried to do but struggled.  Do you have any code you could post to do this?

TIA

Jav replied on Saturday, August 19, 2006

To each of my Objects and Collections I have added:

Public ReadOnly Property BrokenRulesString() as String 
        Get
              ' Use a StringBuilder to Concatenate all BrokenRulesStrings from all Children
        End Get
End Property

If a Collection is not valid, I call its BrokenRulesString to get a listing of broken rules of all its children.  Each object identifies itself in the list of broken rules it sends back, and also inserts line breaks etc as needed.  Empty String comes back if no rules are broken.       

Jav

tetranz replied on Friday, August 18, 2006

If you're showing the collection on a grid then, I don't know about other grids but the Janus grid very nicely highlights objects with broken rules using IDataErrorInfo

Copyright (c) Marimer LLC