Sunday, October 23, 2011

How to implement the workflow sequence pattern in WF4

The Sequence Pattern is used to construct a sequence of consecutive activities, which execution turn one after the other.


Creating a sequence workflow in WF
Using Visual Studio 2010, we start by selecting File-> New Project-> C# section->workflow->workflow console application to create a new workflow. We can name the project SequenceWorkflow.

First, drag a Sequence activity to the designer from the Toolbox. Next, drag two WriteLine activities into the Sequence activity. We can see the following screenshot

If we run the project, without debugging, the result will be:

Creating the first workflow in WF using C# code: Sequence Workflow

For creating a workflow using C# code and using Visual Studio 2010, we start by selecting  File -> New Project -> C# section -> Console Application to create a new project. It is necessary to add a reference to the System.Activities assembly, as already mentioned in "A first workflow with WF4".

Change the Program.cs file to:

    class Program
    {
        static void Main(string[] args)
        {
            WorkflowInvoker.Invoke(new SequenceWorkflow());
        }


        public class SequenceWorkflow : Activity
        {
            public SequenceWorkflow()
            {
                this.Implementation = () => new Sequence()
                {
                    Activities = {
                        new WriteLine() { Text = "Hello Workflow" },
                        new WriteLine() { Text = ""+ DateTime.Today.Day}
                    }
                };
            }
        }
    }
 

As we can see from this example, we set the implementation of this workflow with the delegate that returns an activity that contains the execution logic. In this case, the activities of the defined sequence are two WriteLine activies, that are executed in the defined order.
Notice also that by implementing a class inherited from Activity, we define a workflow using imperative code.

No comments:

Post a Comment