CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

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

Answered (Verified) This post has 1 verified answer | 5 Replies | 2 Followers

Top 500 Contributor
16 Posts
jhardman posted on Tue, Feb 14 2012 6:02 PM

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?

Answered (Verified) Verified Answer

Top 10 Contributor
1,815 Posts
Answered (Verified) JonnyBee replied on Tue, Feb 14 2012 10:34 PM
Verified by jhardman

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.

 

Jonny Bekkum, Norway CslaContrib Coordinator

All Replies

Top 10 Contributor
1,815 Posts
Answered (Verified) JonnyBee replied on Tue, Feb 14 2012 10:34 PM
Verified by jhardman

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.

 

Jonny Bekkum, Norway CslaContrib Coordinator

Top 500 Contributor
16 Posts
jhardman replied on Tue, Feb 14 2012 10:49 PM

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

Not Ranked
3 Posts
John replied on Tue, Feb 21 2012 10:58 PM

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.....

Top 10 Contributor
1,815 Posts
Answered (Not Verified) JonnyBee replied on Tue, Feb 21 2012 11:32 PM
Suggested by JonnyBee

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).

 

Jonny Bekkum, Norway CslaContrib Coordinator

Not Ranked
3 Posts
John replied on Wed, Feb 22 2012 12:20 AM

That works perfectly. Many thanks.

Page 1 of 1 (6 items) | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems