How to run Root business rules when Child collection property changes?

How to run Root business rules when Child collection property changes?

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


jhardman posted on Tuesday, February 14, 2012

I have a situation where I have an editable root with a child collection.  I am trying to work out how to run a rule on the editable root when a property changes on the child.  An example of what I am trying to do is an invoice with line items.  Each line item has a received property.  The invoice has a completed property.  What I want to do is when all line items have been received then set the invoice completed property to true.  In the past I probably would have used events to each line item so the invoice class gets notified to check if all line items have been received.  What is the best way to achieve this with business rules?

JonnyBee replied on Tuesday, February 14, 2012

Hi,

My preferred solution is to override OnChildChanged in the parent object and call PropertyHasChanged(CompletedProperty).

PropertyHasChanged will run the rules for CompletedProperty, mark the object as dirty and reaise OnPropertyChanged to notify UI of changes.

And finally create a rule on the CompletedProperty that checks if all children are completed and the set the property in Parent.

 

jhardman replied on Tuesday, February 14, 2012

Thanks Jonny, you seem to have all the answers :).

JohnFry replied on Tuesday, February 21, 2012

I've tried implementing this for a simlilar requirement, but find that it interferes with "Undo" operations -  CanSave and CanUndo (of ViewModel<Parent>) remain true after undo completes:

Even if there is nothing to undo in the child collection, OnChildChanged is invoked many times during Undo operation (number of items + 1) and using PropertyHasChanged(any parent property) in the overridden OnChildChanged seems to leave the parent believing that it is dirty after Undo completes.

Or is it just me.....

JonnyBee replied on Tuesday, February 21, 2012

You will also need to override these 2 methods in your BO:

    /// <summary>
    /// This method is invoked before the CopyState
    /// operation begins.
    /// </summary>
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    protected virtual void CopyingState()
    {
    }

    /// <summary>
    /// This method is invoked after the CopyState
    /// operation is complete.
    /// </summary>
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    protected virtual void CopyStateComplete()
    {
    }

so that you can set a flag in your bo so that  OnChildChanged should just do nothing and return (now doing an Undo operation).

 

JohnFry replied on Wednesday, February 22, 2012

That works perfectly. Many thanks.

Copyright (c) Marimer LLC