Monday, February 8, 2010

Windows Workflow Foundation Custom Activity - Part 1


Introduction

Windows Workflow foundation gives many activities to use in workflow like code, delay, IfElse, Listen, Parallel etc. But in some scenario, those activities will not work as per our requirement. So we need to implement custom activity which will work as per our requirement.

To create a custom activity, we need to implement one class which is derived from “Activity” base class because every workflow activity is directly/indirectly derived from “Activity” class.

Example

Let’s take one example. We have to create one workflow for Employee management. For this workflow, we need one activity which has some properties like FirstName, LastName, Age etc… (For employee entity) and have one function which will insert employee data into database.

For the above scenario, we need to create our own activity which takes care of above functionality.

Here below I created one class “EmployeeActivity” which is derived from the “Activity” class.

public class EmployeeActivity : Activity

{

}

Now we need to add properties for Employee entities. Here I took only 3 properties for demo purpose.

public class EmployeeActivity : Activity

{

private string firstName;

public string FirstName

{

get { return firstName; }

set { firstName = value; }

}

private string lastName;

public string LastName

{

get { return lastName; }

set { lastName = value; }

}

private int age;

public int Age

{

get { return age; }

set { age = value; }

}

}

In above code, I have created three private variables and public properties for FirstName, LastName and Age. You can add any number of variables and properties which you need.

Now we need to override “Execute” method of “Activity” class. This method will take care of inserting employee data into database.

Let’s implement “Execute” method:

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

/// Code to insert employee data into database

return base.Execute(executionContext);

}

Now you can put your custom activity on workflow and set the property. Whenever this activity will execute, it will insert employee data into database.

Now think that user wants to do some pre authorization before data will store into database. Means you have to implement one event which will execute before employee information will store in employee database.

Let’s create one event and integrate it with EmployeeActvity:

public delegate void PreAuthorizationEventHandler(object sender, CustomActivityEventArgs e);

public event PreAuthorizationEventHandler PreAuthorizationEvent;

Here we created one delegate and one event. We create one class “CustomActivityEventArgs” which is derived from “EventArgs” base class.

[Serializable]

public class CustomActivityEventArgs : EventArgs

{

private string firstName;

public string FirstName

{

get { return firstName; }

set { firstName = value; }

}

private string lastName;

public string LastName

{

get { return lastName; }

set { lastName = value; }

}

private int age;

public int Age

{

get { return age; }

set { age = value; }

}

}

Now we need to change “Execute” method code to execute event. Updated code is look as below:

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

CustomActivityEventArgs customActivityEventArgs = new CustomActivityEventArgs();

customActivityEventArgs.FirstName = FirstName;

customActivityEventArgs.Age = Age;

customActivityEventArgs.LastName = lastName;

//Here we are executing event

if (PreAuthorizationEvent != null)

PreAuthorizationEvent(this, customActivityEventArgs);

/// Code to insert employee data into database

return base.Execute(executionContext);

}

When you will use this EmployeeActivity in you workflow, you will get one event “PreAuthorization” which will execute before employee data inserting in the database.


1 comment:

DotNet Code Guru