February 2006 - Posts

Windows Workflow Foundation - Why WF?

There are four major short-comings of developing workflow applications that WF helps simplify within it's framework...

  1. Ability for the workflows to maintain state for extended periods of time.
  2. Implementing dynamic flow that model human behavior with workflows.
  3. Generating Workflows thru XML & Updating/extending workflows at runtime.
  4. Adding transparency to workflows while workflows are developed and ran.

 WF has a hosting layer that connects the host application running the WF Runtime with the workflow. State for a workflow is managed through the host layer service: "Persistence". Instead of the persistence service provider maintaining state within memory for extended periods of time, the provider knows when a workflow has become idle. Maybe an approval needs to be made and the workflow is waiting for requested data, but instead of holding it's state in memory for the duration, it can persist it's state within a data store whether it be SqlServer or the file system.

 The State-Machine Style for workflows allows the flow of workflows to be more reactive then predefined. Activities need the ability interact through human input. This style of workflow characterizes human behavior where activities within a workflow can become active from any possible activity. Now human behavior workflows are supported using WF and modeled just as efficient as predefined workflows.

 One of the most important features of WF is the ability to create and update workflows at runtime. WF allows workflows to be built using XAML(Extensible Application Markup Language). Workflows can now be dynamically changed by making modifications to its XAML. There is also a namespace, System.Workflow.ComponentModel.WorkflowChanges that allows activities to be manipulated within a workflow during runtime.

 Using WF, developers do not have as deep to see what makes up a workflow. The workflow designer enables drag n drop functionality to construct a workflow quickly. Once a workflow has been created, the code can be stepped through and debugged visually through the designer. There is also a hosting layer called, "Tracking" that allows workflow processing to be traced. The tracking information of a workflow is stored to fit the hosting process.

Implementing Generic List And IEnumerator

In this example, I show how to iterate through a collection of Employees by using the System.Collections.Generic.List and System Collections.Generic.IEnumerator Namespace. System.Collections.Generic.List is similar to System.Collections.ArrayList which we are used to using, however Generic.List allows you to Type-Safe your objects for better performance compared toexpensive processing/poor performance that comes with boxing and unboxing objects. I also decided to throw in how to build a basic generic delegate to show how it could also advantage of a Type-Safe Sender parameter. Let's go ahead and delv into the code. My comments should help you follow how I used the generics 

public class Employee

{

private string _name;

public string Name

{

get { return _name; }

set { _name = value; }

}

private int _age;

public int Age

{

get { return _age; }

set { _age = value; }

}

private string _sSN;

public string SSN

{

get { return _sSN; }

set { _sSN = value; }

}

private string _employeeNumber;

public string EmployeeNumber

{

get { return _employeeNumber; }

set { _employeeNumber = value; }

}

public Employee(string EmpName, int EmpAge, string EmpSSN)

{

Name = EmpName;

Age = EmpAge;

SSN = EmpSSN;

EmployeeNumber = "Employ" + EmpSSN;

}

}

public delegate void EventHandler<T, U>(T Sender, U eventargs); //EventHandler has two placehoders Sender and eventargs. Notice that the are of NO Type! I also commented out the old way of declaring my delegate and event

public class Employees

{

//OLD...public delegate void EmployeeAdded(object sender, EmployeeArgs e);

//OLD...public event EmployeeAdded Added;

public event EventHandler<Employees,EmployeeArgs> AddedEvent; //Type-Safe because I am declaring it with 'Employee' and 'EmployeeArgs'

List<Employee> _employees = new List<Employee>(); //Type-Safe by declaring it as 'Employee'

public void Add(Employee NewEmployee)

{

_employees.Add(NewEmployee);

//OLD...Added(this,new EmployeeArgs(NewEmployee));

AddedEvent(this, new EmployeeArgs(NewEmployee)); //Fire this event once an employee has been added

}

public IEnumerator<Employee> GetEnumerator() //By declaring the IEnmerator we are able to iterate through each employee

{

foreach (Employee Emp in _employees)

{

yield return Emp;

}

}

}

public class EmployeeArgs:EventArgs //Basic EmployeeArgs that inherits from EventArgs

{

public EmployeeArgs(Employee Emp)

{

EmployeeNumber = Emp.EmployeeNumber;

}

public string _employeeNumber;

public string EmployeeNumber

{

get { return _employeeNumber; }

set { _employeeNumber = value; }

}

}

Now that I have my business logic, I created a windows form to model how it works.

public partial class Form1 : Form

{

Employees MyEmployees = new Employees();

public Form1()

{

InitializeComponent();

//OLD...MyEmployees.Added+=new Employees.EmployeeAdded(MyEmployees_Added);

MyEmployees.AddedEvent+=new EventHandler<Employees,EmployeeArgs>(MyEmployees_AddedEvent);

}

//OLD...private void MyEmployees_Added(object sender, EmployeeArgs e)

//{

// MessageBox.Show(e.EmployeeNumber);

//}

private void MyEmployees_AddedEvent(Employees sender, EmployeeArgs e)

{

MessageBox.Show(e.EmployeeNumber);

}

private void button1_Click(object sender, EventArgs e)

{

MyEmployees.Add(new Employee (txtName.Text, Convert.ToInt32(txtAge.Text), txtSSN.Text));

foreach (Employee Emp in MyEmployees)

{

MessageBox.Show(Emp.Name);

}

}

}

SQL & VS2005 Launch Event Hosted By Idea Integration and SQL Server Central

I had the honor of speaking at the launch event hosted yesterday by Idea Integration and SQL Server Central. We had around 250 registered attendees with about 160 or so showing up. My sessions were, "New Features In ASP.Net 2.0" and Windows Workflow Foundation(WF) which has quickly become my new passion. For the ASP.Net session, I discussed Master Pages which is a MUST for every web designer, and the new data controls and how they could be wired up with minimal code. The major points I made were that data controls could richly display data very quickly with no code. I also discussed the new Object Datasource which allows you to wrap custom code for your data component and then configured using the datasource setup wizard... Very cool stuff if you have not checked it out.

I was very concerned about my WF session. I really did not get to do a mock run-through with it and I really did not have any set demos. This actually worked out because I just walked though getting familiar with the different styles of workflows, demonstrating the new WF IDE/tools and going through some code from the top of my head that I thought I would like to see myself if for the first time I was seeing the framework. 

I have put in a ton of work for preparing for this event, and at the end of the day I felt it was all well worth it by helping people understand something new. Well I am off this Friday for South Florida's Code Camp to speak some more about WF and SQL2005 and meet some new friends. If you have not had a chance to register for this free event there is still time!