Recursive State Composition

There may be times in a state-machine workflow when multiple states need to respond to the same event or events. When modeling this type of functionality, developers commonly place the same event in multiple states, which causes replicated behavior. Obviously, this is not an optimal method.

The state-machine workflow type in Windows Workflow Foundation provides a way to implement this behavior in a more elegant manner. Essentially, states that want to listen for a common set of events are added to a parent state. This pattern is referred to as recursive state composition.

Refer back to the order workflow scenario. There might be states called Order Processing and Order Approved, with both states transitioning to an Order Completed state when an external event dictates this move. Figure 10-1 shows an example of what this workflow might look like, with each state taking care of its own transition.

image from book
Figure 10-1

Again, this is not optimal because each state has the same logic as the other for transitioning to the Order Completed state. If both the processing and approved states are wrapped in a parent state that knows how to transition to a completed order, this duplicate logic can be eliminated. Figure 10-2 shows an example of what this might look like.

image from book
Figure 10-2

In recursive state composition, both the parent and child states are standard State activities by definition. A parent is a parent because the workflow developer added other State activities to it. Conversely, a child state is a child state because the developer added it to a parent State activity. However, when a State activity becomes a parent, its default behavior changes. The most glaring difference is the fact that a parent state cannot be directly transitioned to. Rather, transitions are made to child states, thereby activating the parent state. However, the parent state can have EventDriven activities just as a standard State would. Subsequently, while the workflow is in any of the child states, the parent’s events will be raised and executed just as though the child state owned the event.

The concept of recursive state composition is commonly used to model a workflow’s exception-handling scenarios. In this case, the word exception is referring not to a .NET exception but to a process exception. For example, if at any time during a workflow something goes wrong, it can enter a state called On Hold and notify a support agent of the issue. After the issue is resolved, the workflow can resume.

Professional Windows Workflow Foundation
Professional Windows Workflow Foundation
ISBN: 0470053860
EAN: 2147483647
Year: 2004
Pages: 118
Authors: Todd Kitta

Similar book on Amazon
Pro WF: Windows Workflow in .NET 4 (Expert's Voice in .NET)
Pro WF: Windows Workflow in .NET 4 (Expert's Voice in .NET)
Pro WF: Windows Workflow in NET 3.5 (Expert's Voice in .NET)
Pro WF: Windows Workflow in NET 3.5 (Expert's Voice in .NET)
Microsoft  Windows  Workflow Foundation Step by Step (Microsoft Windows Step by Step)
Microsoft Windows Workflow Foundation Step by Step (Microsoft Windows Step by Step)
Essential Windows Workflow Foundation
Essential Windows Workflow Foundation © 2008-2017.
If you may any questions please contact us: