Windows Workflow Foundation
I was talking to an associate of mine at work on Friday about a workflow tool that she was gathering requirements for. It was a rather simple process flow, but as this is something that could be extended for other clients, it would make sense not to code our own rules engine.
After swinging around Google a bit, I remembered something that I had read in MSDN Magazine a few months ago about Windows Workflow Foundation. I found it intriguing how simple it seemed to generate and process a workflow. So, Friday night (I know, my life is exciting), I found myself parked in front of my PC to install all of the prerequisites for .net 3.0 (formerly WinFx) and the Windows Workflow Foundation extensions for VS.net 2005. After a few minor hiccups, I had everything working beautifully.
Tonight, I finally had a chance to set up a sample project and start working through the world of WWF. I chose to create a console application. The first thing I did was create a sample workflow (see below):
After the workflow, I started configuring the console application itself to initiate the workflow. The code for this is extremely simplistic and makes A LOT of sense (at least to me). The first event that gets run in the above workflow calls out to a web service method. Configuring it to use the web method was simply point and click. Incredible! You assign properties to the method parameters and that's really all there is to it.
The Web Method name was "InitiateExpense" and accepted a parameter "Name" as type String and another parameter "Amount" as type Decimal. I chose to name them in the context of this workflow as "InitiateName" and "InitiateAmount". The Web Method returns a String value that I named "InitiateReturn".
Within the console application, the code that actually starts this workflow - and passes in the Name and Amount parameters is like this:
Dim parms As Dictionary(Of String, Object)
parms = New Dictionary(Of String, Object)
parms.Add("InitiateName", name)
parms.Add("InitiateAmount", amount)
Dim workflowInstance As WorkflowInstance
workflowInstance = _
workflowRuntime.CreateWorkflow(GetType(Workflow1), parms)
workflowInstance.Start()
WaitHandle.WaitOne()
As you can see, it's pretty darn simple. Using template classes within .net is very simple and easy as well. I'm really not sure how custom classes would work here, but since the Workflow above is simply a transport mechanism and the custom object would (of course) inherit from System.Object, it should work without a problem.
In any case, as with most workflows, now it's simply a matter of saying "If it's approved" then do this, otherwise, do this. That type of conditional statement would normally be hard, but not with WWF. You simply add a "Conditional" object from your toolbox and configure the True statement - in my case:
this.InitiateReturn == "true"
Even when you specify the "True" condition, there's intellisense, so it was very easy to specify the property above. After that, you add the task for each branch and you're done.
Even when you want to retrieve values from the completed workflow, you just handle the event OnWorkflowComplete as seen below:
Shared Sub OnWorkflowCompleted(ByVal sender As Object, _
ByVal e As WorkflowCompletedEventArgs)
Dim x As String = _
Convert.ToString( _
e.OutputParameters("InitiateReturn"))
System.Console.WriteLine(x)
System.Console.ReadLine()
WaitHandle.Set()
End Sub
As you can see, it's extremely simple and I'm totally digging it. I just wish it wasn't in beta! Oh well, this project probably won't get kicked off until early next year so I've got some more time to play.