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

Health Framework - Apple vs Android

In the past few years we have seen mobile and its apps rise and shine transforming many industries in its wake. However, the growth in health and fitness category has been less spectacular at 49% compared to overall mobile app industry which grew at 115% in the year 2013 (source Flurry Analytics). A few years ago Microsoft and Google attempted to make inroads into the health and fitness sector by bringing web based products to store and maintain health and fitness information like MS HealthVault and Google Health with not so spectacular results. Next came innovations by Fitbit in the wearables sector for activity tracking, in 2011 and 2012 they introduced first wireless activity trackers to sync using Bluetooth. This was followed by the entry of Jawbone into health sector with its announcements of Up wristband and accompanying app. These have had better success resulting in many startups joining the wearables product bandwagon.  Late to the stage almos...

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 ...

Quick notes on Git

  I have been away from writing anything for a long time and instead have been fooling around with other stuffs like just plain reading, growing mustache, trying to learn swimming, trying to learn to play acoustic guitar and trying my hands at photography. To be honest I have not given up on them yet but neither have I been able to hang on to them in a disciplined manner. So here I am back to my writing after a long gap. This time its going to be quick notes on Git . Git is a file repository. As opposed to other repositories like SVN Git thinks of its data more like a snapshot of a mini filesystem. All operations in Git are local. (Entire history of the project is stored locally in your working directory) Browsing project history. Viewing all changes to a file. Git uses Checksum to track repository items Everything in Git is check-summed It uses SHA-1 hash for generating check sum values All a...