Chapter 2 – Built-in Flow Control Activities (Prart 1)

MS WWF 4.0 Cookbook (P. 41) 02/17/2011, 06:40PM

Flow is the center of WorkFlow. How to conotrol the Flow
is what we will see in this chapter. In WF4 we have many concepts (simlar to C#) such as if-else, foreach, switch, try-catch and so on.

WF4 supports (1) Sequence workflow and (2) flowchart workflow. State machine workflow will be available in .NET 4.5.

Using the Foreach Activity

Create a workflow consol application, “UsingForeachActivity”.

Add a class: Person.CS. Name, Age, Initializer, and ToString()

Build the solution, and in WF Designer, import the UsingForEachActivity namespace.

Declare a variable of List of Person class (List<Person>)

Select List<T>, then browse the list type, select Person.

In the end: people is a generic list of Person objects.

Enter this code in the default expression of the variable people. (to initialize?)
Currently, we can only use VB Expressions.


inside Sequence, add a ForEach activity. Type of Argument is UsingForEachActivity.Person.

A Number guessing game in Sequence

New Workflow Console app, named “GuessnumberGameInSequence”.

Add c ode file, build it, and it will be available in ToolBox.

Note: Difference between CodeActivity and NativeActivity

useful in creating custom code and activities. When you create your own code, you can inherit either one.

CodeActivity:
(1) provides an Execute method that canbe overwritten to have your implementation. Provide access to tracking, variables and argumnets.
(2) use CodeActivity for simple custom activities
(3) Use CodeActivity for Sync activities.
(4) Use when you need to execute a single pulse of execution.
(5) Use when you do not need to schedule other activities.
(6) Use when you do not need to use advanced WF runtime features.

NativeActivity:
(1) With all features of CodeActivity, plus (a) abort activity execution, (b) cancel child activity execution, (c) schedule activities, (d) use bookmarks, (d) use activity actions and functions.
(2) Complex custom activities
(3) for long running custom activities
(4) when you need to execute multiple pulses
(5) when you have to schedule other activities
(6) when you need to used advanced WF runtime features.

Unless needed, use CodeActivity.

A Number Guessing Game using a FlowChart

Create the same game using flowchart. Will demonstrate the usage of the FlowDecision activity.

new Workflow console application, “GuessNumberGameInFlowChart”. Add a Code file, ReadNumberActivity.CS (same as previous practice), build the code, and it’s available in the Toolbox.

Drag a FlowChart to WF Designer.

Start dragging activities to the Designer. There are connection points in each object and you can drag and connect them.

Drag a FlowDecision when you need to make a decision:


Each FlowDecisin has a Condition to decide which way it should go

This is very powerful yet easy and very intuitive….

Running:


So two ways:
(1) Sequence with IF-Else
(2) FlowChart with FlowDecision.

Which one to use????? If we have a lot of backward transitions, then use FlowChart.

Using InvokeMethod Activity

Use this method to invoke various kinds of activities. Invoke a method in a class file.

Create Workflow Console Application “UsinginvokeMethodActivityInCode”.

Cretae a class TestClass.cs with some methods in it:

Here is a simpler example:
A simple class file with a VOID method

Drag a InvokeMethod activity, and set propertyies

Run it.

Another sample (with parameters)

Since this method accepts two parameters, set up parameters, run it

Another one – with parameters, and with a return type


(Page 60)

Another one – method with in-parameters, and a out (reference) parameter

Everything the same, except add resultVar as a In/Out variable

Another example: generic type (meaning any type)

In GenericTypeArgument, enter all known types:

InvokeMethod a static method:


Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: