Workflow with WF
This week I've been getting into WF1 by adding some approval workflow to the CS 2.0 Articles. Don't get excited, this WON'T be checked-in people. It's purely research.
So far, I'm impressed. Ken now realizes that he approached it the wrong way the other day. Workflow is not intended to replace all loops and conditional statements in your code. The way I view it is that applications will become intelligent libraries of functionality that can be glued together with workflow.
Workflow is the answer to the question "Then what happens?". Use cases are workflow.
The beauty of WF is that our users can plug in different workflows into our products. They can edit our workflows or create their own. Third parties can create them, and I suppose sell them as enhancements in their own right.
(Actually the "edit our workflows" is something that looks interesting since I've read that MSFT allow you to ship the WF design surface in our products, so our users really can graphically modify workflows. Winforms of course, although wouldn't it be nice of Microsoft to supply a web-friendly editor for our Control Panel?)
So, in my tests I added a method SeekApproval that was called when an article was created or updated. To make it easy for myself I put all the code inline (eventually there will be Workflow support classes to start/stop/persist, etc), so to start the WF runtime:
#region start workflow engine
if (workflows == null)
workflows = new WorkflowRuntime();
if (!workflows.IsStarted)
workflows.StartRuntime();
#endregion
I added code in there to persist the workflow using the SqlWorkflowPersistenceService too, but I haven't finished a complex-enough workflow to test that with yet. Plus, I've just read that all of this could/should be set up in web.config's new <WorkflowRuntime/> section. Oh well, best practices will come in due course. 
To start the approval process you just start a new workflow:
WorkflowInstance myWorkflow = workflows.CreateWorkflow(workflowType, p);
myWorkflow.Start();
And, uh, that's it. The workflow does whatever you've defined. It might send notifications via email, IM, SMS, fax, skype or decoder ring to your site admins and wait for them to review it and choose Accept, Reject, Return for edit, Delete, Mark as spam and Ban the user or who knows what.
Or as in my case it could just instantly call Content.Approve() with no interaction at all. Hey, I didn't say I was finished.
Ok, so I cheated a little above. I didn't show where workflowType or the self-documenting 'p' came from. So here you are:
// load the relevant workflow using reflection
Assembly asm = Assembly.Load(workflowAssembly);
Type workflowType = asm.GetType(workflowClass);
Dictionary p = new Dictionary();
p.Add("Content", content);
p.Add("ForUpdate", forupdate);
I load the workflow from an assembly whose name that we store somewhere like a config file or db settings table. But the nice part is how we pass parameters into the workflow. See that p.Add("Content", content) line? That passes in content as the value for the public Property called Content in your workflow!
So, I have this property in my workflow class:
public CommunityServer.Components.Content Content
{
set { _content = value; }
}
and the parameter is effectively calling workflow.Content = content. Hence, inside my workflow I can make decisions on content and act on it.
The only thing that I'm not quite sure about yet is that we've effectively created a contract that any workflow that is used for content approval must have these properties.
How to best enforce that? I suppose we look for an attribute on the class we're told to load?
The next step for me is to complete work on the ComplexApproval workflow class. It's a StateMachineWorkflowActivity that
- Sends a different email to admins depending on whether it is new or updated content.
- The workflow gets persisted and unloaded.
- When a link is clicked in the email it calls a web service that raises an event that wakes up the workflow.
- If 4 or more people have approved it the content will be approved and the workflow terminated. (Silly I know, but I'm trying out different functionality here)
BTW, the orange book is nice and I do recommend it. However, there are some changes in the current Beta 2 that break the examples in the book. It's an overview, then head on over to the comprehensive docs installed into VS 2005.
1 Windows Workflow Foundation. I'm going to try and help Microsoft by using the official acronym, although WWF is going to be a hard one to stop people using, no matter what the legally correct answer is. The WWF trademark is tightly held and enforced. (via Brian Loesgen)