Wednesday, April 28, 2010

The WorkflowRuntime Pattern

.Net WF facilitates the idea of decoupling business activities and the overall controlling logic. In that model, specific granular business activities are developed in terms of workflow first, and then they are assembled into an application with the medium of WorkflowRuntime.

Technically WF brings a pattern how to integrate workflow oriented development. Specifically, an application holds a WorkflowRuntime (Sure it holds multiple runtimes on branched threads) and a signal(s) for synchronization with the main thread of hosting application. By assigning a WorkFlow type and a callback handler delegate, the runtime then creates the workflow instance, executes on it, and call the callback delegate upon finishing. A sample code shows on List 1.

List 1: Use WorkflowRuntime synchronization

using (WorkflowRuntime r = new ~()){
AutoResetEvent waitHandle = new ~();

r.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e){
waitHandle.Set();

}

WorkflowInstance ins = r.CreateWorkflow(typeOf(WorkflowApp.Workflow1));
ins.Start();

waitHandle.WaitOne();

}

A WorkFlow instance runs on a backend thread, where it can be synchronized with the main thread or other threads with WaitHandlers.

No comments: