Sunday, July 20, 2008

Custom Activity For Windows Workflow

As you know Activity is a unit of work which could be simple or complex. Workflows use all these activities to finish its job, so we can say that Workflow is a collection of activities. In the design mode you see Base Activity Library in tool box those are all core collection of activities that come built in.

Why you need a Custom Activity!? As simple as you want to build once and reuse again and again the object or the way you process the data/flow. Simple Example Writing the status notifications back to DB over and over as the Workflow process moves/jumps the state. Or moving the files around workflow.


How to Build a Custom Activity!? Derive from Base Activity, Override Execute Method and Use Properties if needed. You can also use Dependency Properties.

What is a Dependency Properties!? Unlike normal properties, it won’t take up memory in the instance. It’s more of Centralized storage, Declarative binding at design time and also integrated with Workflow.

Code Snippet for DependencyProperty (<Shortcut>propdp</Shortcut>):
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Define a DependencyProperty</Title>
<Shortcut>propdp</Shortcut>
<Description>
Code snippet for a property using DependencyProperty as the backing store.
</Description>
<Author>Raja Dandu</Author>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>type</ID>
<ToolTip>Property Type</ToolTip>
<Default>int</Default>
<Function>
</Function>
</Literal>
<Literal Editable="true">
<ID>property</ID>
<ToolTip>Property Name</ToolTip>
<Default>MyProperty</Default>
<Function>
</Function>
</Literal>
<Literal Editable="false">
<ID>ownerclass</ID>
<ToolTip>
The owning class of this Property. Typically the class that it is declared in.
</ToolTip>
<Default>ClassNamePlaceholder</Default>
<Function>ClassName()</Function>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[
public $type$ $property$
{
get { return ($type$)GetValue($property$Property); }
set { SetValue($property$Property, value); }
}

//Using a DependencyProperty as the backing store for MyProperty.
//This Enables Animation, Styling, Binding.
public static readonly DependencyProperty $property$Property =
DependencyProperty.Register("$property$", typeof($type$), typeof
($ownerclass$), new UIPropertyMetadata(default($type$)));


$end$]]>

</Code>

</Snippet>

</CodeSnippet>

</CodeSnippets>

Choose the Activity Template in Visual Studio which will give you Design Surface whereby you can drag N drop built in Activities to build Custom Activity or build purely using code which can be active by implementing the “Activity” Interface. Then create your properties if needed and then override the “Execute” Method.

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
try
{
WriteStatus(Source, Connection, Message, Priority);
return ActivityExecutionStatus.Closed;
}
catch (Exception)
{
return ActivityExecutionStatus.Canceling;
}
}
Use the Try catch bolck to trap any failure, If failed return cancel. If needed you can use “componsation” for compensating the halfdone Work/Process.

If successful:
return ActivityExecutionStatus.Closed;

Once you sucessfully build the project Activity calss, Your Custom Activity will apper in yout Main Workflow toolbox.





After creating multiple custom Activities, you can combine them into one single Activity based on your requirement. This Activity called as Composit Activity.