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?
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.
Thanks Jonny, you seem to have all the answers :).
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.....
You will also need to override these 2 methods in your BO:
/// This method is invoked before the CopyState
/// operation begins.
protected virtual void CopyingState()
/// This method is invoked after the CopyState
/// operation is complete.
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).
That works perfectly. Many thanks.
Copyright (c) Marimer LLC