Skip to main content

Workflow Foundation 4 - Part 3 - Data storage and management

This is my third post on WF4. First one was an introductory post on WF4 and in second one we focused on executing workflows. In the this post I am going to focus on the topic of data storage and management.
Every business process or flow depends on data. When you think of data there are three elements to it as listed below -
  • Variables - for storing data
  • Arguments - for passing data
  • Expressions - for manipulating data.

Let us first look at the variables. Variables are storage locations for data. Variables are declared before using them just like in any other languages like C# or VB.Net. Variables are defined with a specific scope. When you create a variable in an activity the scope of the variable becomes that activity's scope. Variables can also have access modifiers like None, Mapped or ReadOnly. Let us look at an example where we will create two variables and assign a scope to them along with access modifiers.




//Declare a sequence activitiy
Sequence seqWf = new Sequence();


//declare a variable of type string with default value = Hello
Variable vGreeting = new Variable("Greeting","Hello");
//specify access modifier as readOnly
vGreeting.Modifiers = VariableModifiers.ReadOnly;


Variable vGreetingPart = new Variable("GreetingPart", " World");
vGreeting.Modifiers = VariableModifiers.ReadOnly;


//add variable to variables collection of an activity
seqWf.Variables.Add(vGreeting);
seqWf.Variables.Add(vGreetingPart);


//invoke the workflow
WorkflowInvoker.Invoke(seqWf);




In the above code sample we are creating two variables vGreeting and vGreetingPart. Both these variables are of type string Variable. They are then provided an access modifier using the Modifiers property of the variable. Finally this is added to the collection of variables of an Activity. This last action is what defines the scope of the variable.



Next is arguments. Arguments are defined on activities. They help define the flow of data into and out of an activity. Arguments to activity are like arguments to methods. Arguments have a name, type and can have following direction - In, Out and InOut. Workflow runtime ensures that when an activity is about to start its execution all its In and InOut arguments are evaluated and available for use even if a get function is not called on the argument. When a set function is called on the Out argument it is set immediately. Arguments can optionally specify an evalutaion order. This order is zero based and is tackled in an ascending order. Let us look at an example of how In and Out arguments are handled in a simple activity like Assign Activity.


//Declare a sequence activitiy
Sequence seqWf = new Sequence();


//declare a variable of type string with default value = Hello
Variable vGreeting = new Variable("Greeting", "Hello");
Variable vGreetingDuplicate = new Variable("GreetingDuplicate");


//declare an assign activity
Assign assignment = new Assign();


//specify the value to assign or input argument
assignment.Value = new InArgument(vGreeting);


//specify the variable to set or output argument
assignment.To = new OutArgument(vGreetingDuplicate);


//add variable to variables collection of an activity
seqWf.Variables.Add(vGreeting);
seqWf.Variables.Add(vGreetingDuplicate);


//add activity to activities collection of the root activity
seqWf.Activities.Add(assignment);


//invoke the workflow
WorkflowInvoker.Invoke(seqWf);




In the above example we created two variables one vGreeting and another vGreetingDuplicate. Both of string type. Then we created an activity called Assign to handle string assignment. This activity defines one In argument of type string and another Out argument of type string. We supplied vGreeting variable to the In argument of the assign activity (assignment.Value = new InArgument(vGreeting)) and provided vGreetingDuplicate variable to its Out argument or target (assignment.To = new OutArgument(vGreetingDuplicate)). Finally we added the variables to variables collection of the root sequence activity and assign activity to activities collection of the root sequence activity.
We will discuss passing arguments in and out of a workflow in a separate post.

Lastly we have the expressions. Expressions are activities that you can use to operate on data. You can use expressions to set arguments. Expressions derive from Activity and return a value of specific type which means they are activities that have an OutArgument property named Result. Let us take a look at an example where we use a VisualBasicValue expression to concatnate a string literal with value from a variable and store it in another variable using an assign activity.


//Declare a sequence activitiy
Sequence seqWf = new Sequence();

//declare a variable of type string
Variable vGreeting = new Variable("Greeting");
Variable vGreetingPart = new Variable("GreetingPart", "Hello");


//declare an assign activity
Assign assignment = new Assign();


//specify the variable to set or output argument
assignment.To = new OutArgument(vGreeting);


//specify the value to assign or input argument
assignment.Value = new InArgument(new VisualBasicValue("GreetingPart + \" World \""));


//add variable to variables collection of an activity
seqWf.Variables.Add(vGreeting);
seqWf.Variables.Add(vGreetingPart);


//add activity to activities collection of the root activity
seqWf.Activities.Add(assignment);


//invoke the workflow
WorkflowInvoker.Invoke(seqWf);




Here we are manipulating the data before assigning it to a variable using a VisualBasicExpression - assignment.Value = new InArgument(new VisualBasicValue("GreetingPart + \" World \"")). Here a literal value " World" is concatenated with value already stored in variable vGreetingPart before assigning it to the variable vGreeting using the Assign activity. Note here double quote is escaped using a back slash (\). Also note that variables within an expression is used by the name given to them during initialization using its constructor - Variable vGreetingPart = new Variable("GreetingPart", "Hello").


There are many more expressions and you can create your own expression activities. Some basic expressions are -

  • VariableValue
  • VariableReference
  • VisualBasicValue
  • VisualBasicReference

Note - VisualBasicValue expression use visual basic syntax even when you are using c# to develop your workflow and its activities.

Comments

Popular posts from this blog

Notes on Castle MonoRail

  Sometime back I was doing a small POC on Castle MonoRail. So here are my quick notes on this. MonoRail is an MVC Framework from Castle inspired by ActionPack. MonoRail enforces separation of concerns with Controller handling application flow, models representing data and View taking care of the presentation logic. To work with MonoRail you need Castle Assemblies. It also utilizes nHibernate You can use Castle MonoRail Project Wizard or create the project manually. Project structure – Content Css Images Controllers HomeController.cs Models Views Home \ index.vm Layouts \ Default.vm ...

Introduction to Workflow Foundation 4 (WF4)

I finally decided to pick-up my blogging once more. Since I have been trying to learn Windows Workflow Foundation 4 (WF4) I thought I might as well start off with this topic. WF4 is a development framework that enables you to create a workflow and embed it in a .Net application. It is neither an executable application nor a language. It provides a set of tools for declaring a workflow, activities to create your logic and to define control flow and a runtime for executing the resulting application definition. It also provides services for persistence of state, tracking and bookmarking. You can create your workflow directly in code, in mark-up or in a combination of both. Workflow provide us with two major advantages - Creating unified application logic. Making application logic scalable. Workflow Authoring styles - Sequential Workflow executes a set of contained activities in a sequential manner. Workflow Foundation was introduced in the .Net 3.0 and updated in 3.5. In .net 4 it has bee...