RE: Validation of collection

RE: Validation of collection

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


RockfordLhotka posted on Wednesday, June 21, 2006

It is important to remember that IsValid is not an event - it is a property. And that property is almost never actually called, because it isn't part of data binding
or anything like that. In fact, if you don't write code to call it, it won't be called - and most people never do call it.
 
This is why it implements the task by looping through the child objects - no need for complexity to solve a problem that doesn't really exist. There'd only be a problem if you have a lot of child objects (thousands) and you call IsValid very often.
 
Rocky


From: BigRon [mailto:cslanet@lhotka.net]
Sent: Tuesday, June 20, 2006 10:10 AM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Validation of collection

Rocky - I was trying to extract from this thread an answer to this question:  What is the best practice for checking validity among items (child objects) within a collection from the parent object?  Book examples show overriding the base "IsValid" method with mybase.IsValid and mResources.IsValid.  The problem is that using that example does NOT raise validation exceptions if a child object of that child collection is not in accordance with a validation rule.

Are you recommending looping through the child collection items and checking .IsValid for each item to raise the IsValid flag of the collection to false?  I thought that the framework was designed to "bubble up" these kinds of events. 



--
View this message online at: http://forums.lhotka.net/forums/thread/2070.aspx
--

DansDreams replied on Wednesday, June 21, 2006

Whoah there with those assumptions Rocky.  IsValid is called by IsSavable, which I believe was added specifically because many of us found it very powerful to do things like bind IsSavable to a Save button's Enabled property.

I'd agree that the issue is probably not significant until the collection reaches thousands of elements, but I think you should count on IsValid being called "often" not being so unusual.

esteban404 replied on Wednesday, June 21, 2006

Big Ron,

When you start down the "find the documentation" trail, look for these two threads among many in the old forum:

http://www.searchcsla.com/Details.aspx?msn_id=8264
http://www.searchcsla.com/Details.aspx?msn_id=5525

The best way to get what you want is by using an observer pattern either home-brewed or with Petar Kozul's ActiveObjects version of the CSLA framework in 1.5 and 2.0 flavors.

You're not going to get much more help than that, really, but framework issues are discussed very well here, albeit you'll be left twisting on some topics that were discussed ad nauseum. Some kind souls will sometimes jump in and tell you.

I'm originally UI design only (hey Mac people draw stuff) and I for one didn't know how it affected me until I got several huge projects dumped on me with short time frames and a manger who thinks everything is MSAccess-like. Catching what the error actually is has begun to help me greatly.

They just don't teach this stuff in MCAD/MCSD classes. :-\

_E

RockfordLhotka replied on Thursday, June 22, 2006

Fair enough Dan Smile [:)]

But then I should point out that IsValid is Overridable/virtual in both BusinessBase and BusinessListBase, so if it does become a performance issue for you, you can always override the behavior. In particular, you could override it in your collection class to use a pre-cached value. That pre-cached value could be generated by listening for PropertyChanged events from the child objects and using that notification to keep the value up to date as each child changes.

The easiest way to do this is to override OnListChanged. BLB already handles the child objects' PropertyChanged events and automatically raises ListChanged to notify the UI and/or parent of the change. You can override OnListChanged within your collection to also get this notification, and that could be your trigger to update IsValid. Actually that is the only good way to do it, because it would also cover the delete and clear cases...

Still, it is probably only worth this extra effort in the case that there's an actual performance issue that you are facing, otherwise it seems just like extra work.

Copyright (c) Marimer LLC