Rules on a collection

Rules on a collection

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


ajj3085 posted on Tuesday, February 05, 2008

Hi,

I know this has been brought up before, and I have a problem with a bit different twist.

Basically, I have a document (a Free of Charge) which must ensure that its total ends up being 0.  So I set up a rule on the root Free of Charge document.  It listens for its LineItems child BusinessListBase to raise events and then fire the PropertyHasChanged event for the Total property, which the rule is bound.

The problem is during create; the LineItems needs to know the parent document it belongs to.  The problem is that during the creation of the LineItems instance, the LineItems notifies the Document that it needs to check the Total property.  Since Total on the document is actualy gotten from the LineItems, and the LineItems has not been constructed (and thus the instance field in Document is still null) I end up with a NullRefException.

Originally, I didn't have this and relied on just the PropertyChanged to bubble up to the document.  The problem with that was that I turn off RaiseListChanged events on LineItems for peformance reasons during adding and removing items.

So.. what's the best way to handle this?  I'm at a loss now..

Thanks
Andy

stefan replied on Tuesday, February 05, 2008

Couldn't you just hook the ListChangedListener right after the child list has been initialized?
This would mean you could not initialize the list at declaration time but would need the Initialize Sub or something similar...

Or you could create your own ListChangedEventArgs, providing a reference to the list...

Or you stick with your old solution and just call PropertyHasChanged("whatever") right after each addition/removal of an item...

Stefan

david.wendelken replied on Thursday, February 07, 2008

The list knows which records have been changed.

Don't the rules get checked at save time?

If the list item has a property that is a link to its parent list and also has a rule loops thru the parent list checking the total for the document, wouldn't that do the trick?

 

david.wendelken replied on Thursday, February 07, 2008

Oh, of course, that would also mean you can't save every time a line item is changed, you can only save when a document is "done".

ajj3085 replied on Friday, February 08, 2008

I fixed this, but maybe not in the best way.  If RaiseListChangedEvents is set to true when it was previously false, it immediately raises the list changed event.  Works.. but I'm not 100% happy with it.

Copyright (c) Marimer LLC