Building Workflows

Intertech Tutorials

Building a Simple Workflow

To illustrate the overall flavor of WF development, you will start with a sequential workflow. The goal is to build a workflow that prompts the user for his or her name and validates the results. If the results do not jibe with the business rules, you will prompt for input again until successful.

To begin, assume you have created a new Sequential Workflow Console Application project. Note the name of your WF application is UserDataWFApp.

wf11

The design surface maps to a code file named by default ‘Workflow1’. Assume you have renamed this WF to the more fitting ProcessUsernameWorkflow.

The initial class extends the SequentialWorkflowActivity type. As you add and configure activities, InitializeComponent() will be updated automatically. Notice how the C# constructor calls InitializeComponent(). The initial VB code places the call to InitializeComponent() in the designer-maintained partial class.


// C#
public sealed partial class ProcessUsernameWorkflow :
SequentialWorkflowActivity
{
public ProcessUsernameWorkflow()
{
InitializeComponent();
}
}



' VB
Public class ProcessUsernameWorkflow
Inherits SequentialWorkflowActivity

' InitializeComponent() is within the designer maintained file.
' When you add custom constructors,
' be sure to call InitializeComponent()
' as the first code statement.

End Class

Assume you have added a Code activity to the designer, which has been renamed to ShowInstructionsActivity. The Code activity enables you to define a block of custom code statements to execute within the WF. The ExecuteCode member must be set via the Properties window to specify the name of the method the runtime engine will invoke.

The method assigned to ExecuteCode must match the signature defined by the System.EventHandler delegate. Therefore, the method must take a System.Object as the first parameter and System.EventArgs as the second. If you type in the name of a new method, the IDE will generate stub code once you press the Enter key. Assume the method is named ShowInstructions.

wf12

At this point, your designer looks something like the following. Right-clicking on the designer surface will enable you to bring up the related code file.

wf13

The ShowInstructions() method will show instructions to the user. VB code would be very similar.


// C# (VB code would be similar)
private void ShowInstructions(object sender, EventArgs e)
{
// VB: Dim previousColor As ConsoleColor = Console.ForegroundColor
ConsoleColor previousColor = Console.ForegroundColor;

Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("*******************************************");
Console.WriteLine("***** Welcome to the first WF Example *****");
Console.WriteLine("*******************************************");
Console.WriteLine("I will now ask for your
name and validate the data...");
Console.ForegroundColor = previousColor;
}

Recall that your WF will continuously ask the user’s name until the input meets the business rule’s criteria. For the sake of this example, the rule is that the input data cannot be longer than 10 characters.

The While activity allows you to define a set of related activities that will continuously execute until a specified condition is true. ‘While’ activities can use a Code Condition to establish a method that will determine whether the loop should terminate. This is set using the Condition properties window. You will call the method GetAndValidateUserName.

wf14

This method must take a System.Object as the first parameter and a System.Workflow.Activities. ConditionalEventArgs as the second. The Result property of ConditionalEventArgs allows you to return true or false. This will inform the WF runtime whether the loop should continue.

The GetAndValidateUserName() method will do the work of ensuring the input data is less than 10 characters. Notice that you are representing the name of the user via a public property and private backing field. Currently, you are hardcoding the maximum number of characters (10).


// C#
public sealed partial class ProcessUsernameWorkflow:
SequentialWorkflowActivity
{
...
// User name field and related property.
private string m_userName = String.Empty;
public string UserName
{
get { return m_userName; }
set { m_userName = value; }
}

// This will be called when the WF enters the While activity.
private void GetAndValidateUserName(object sender,
ConditionalEventArgs e)
{
Console.Write("Please enter name, which much be less than 10
chars: ");
userName = Console.ReadLine();

// See if name is correct length and set the result.
e.Result = (UserName.Length > 10);
}
}

In terms of VB, the While activity event handler would look as follows. The UserName property would function identically to the previous C# code.


' VB
' This will be called when the WF enters the While activity.
Private Sub GetAndValidateUserName(ByVal sender As Object, _
ByVal e As ConditionalEventArgs)

Console.Write("Please enter name, which much be less than 10 chars: ")
m_userName = Console.ReadLine()

' See if name is correct length and set the result.
e.Result = (UserName.Length > 10)
End Sub

The final task to complete your While activity is to add at least a single child activity within the scope of the While activity. This will represent the logic to execute over each iteration of the loop.

Here you will add a new Code activity named NameNotValidActivity. This activity has been connected to a method named NameNotValid via the ExecuteCode member. At this point, your designer should look like the following:

wf15

The implementation of NameNotValid() is intentionally simple. Notice that you are currently hardcoding the error message. You will make this more dynamic in just a bit.


// C#. VB code would be similar.
private void NameNotValid(object sender, EventArgs e)
{
Console.WriteLine("Sorry, try again...");
}

If you were to run this application, you would find yourself prompted continuously until the business rule is satisfied. Here is a possible test run. Again, notice that the user is continuously prompted until the input is less than 10 characters in length.

wf16

Copyright (c) 2008-2013. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.