20 November 2007

Window Workflow Foundation - A Simple Case Study, what does it offer?

To be honest, workflow engine is a concept hard to digest. If you haven't used it before, then it's probably hard to imagine where to use it and why you should use it. During my previous project, I had the pleasure of using a workflow engine developed by Cardiff, it's call Liquid Office (v4.1). While the development experience with it was not so great (and I'll explain what and why later on),  it definitely helped me understood the benefits of using a workflow and jump started my interest in .NET Windows Workflow Foundation.

Let's talk about the requirement.

Imagine you are developing an ASP.NET application for people with disabilities to apply for benefits.  The users of the application have many different roles. Applicant, Office Clerks,  Doctor, Lawyers, Chief Officer, President, Insurance Underwriter, Hospital Staff, etc. Each role needs to approve the application for it to get processed and move onto the next role.  On top of it, there are many complicated business logic related to each role. For example,

  • Applicant fills in the application and it goes to office clerks. If office clerks approves it, then it goes to the doctor. Otherwise, return to the applicant.
  • Doctor needs to make decisions based on applicant's symptoms in order to give it a rating from 30%-100%. Underwriter needs to see if the applicants is a civilian or military,  if they requested a lawyer or not, etc.
  • Based on overall rating of everyone, a price range is estimated and forward back to applicant for review.

Traditional Approach

Now, traditionally, we would write all these business logic inside our business layer. If there are any changes, then we change the code and re-upload it. What do we need a workflow engine for? 

Here are a couple of  issues

  • The flow is somewhat hard wired. In the above example, Applicant goes to Office Clerks, Office Clerks goes to Doctor, Doctor goes to either Lawyer or Chief Officer, etc.  What happens if you want to remove certain roles or insert certain roles in between the approval process? 

    Say the new workflow process demands that after the applicants applies, it goes directly to Doctor, we will skip the Office Clerk. What happens to the processes already forwarded to Office Clerks?

  • Okay. You might say the above reasons are not a big deal because to the first issue, you can configure some XML files or store the flow in a database (Remember now, this is the actual work you have to do, essentially writing an engine yourself) and for the second issue, you can write a script and convert all applications which already forwarded to Office Clerk and now change them to Doctor. Well, let me raise another issue.  

    Before the changes, you already processed 4000 cases, among which, there are 3000 cases still in progress. The requirement is that the existing 3000 cases still use the old process, but the new applicants will start to use the new process. How would you resolve this issue with your home-made configuration?

  • Say the management made a new decision and you need to change how the business flow works for maybe only around 500 applicants out of the 3000 active cases out there based on certain new requirements?

  • Your code is tightly bounded to the workflow.  If the workflow changes significantly, or changes frequently, then you will have a heck of time maintaining your code around it.


Hopefully, the above issues raised your eyebrows.  I've asked some questions on the Windows Workflow Foundation forum in regards to these issues and they can be summarized as the following

Technorati Tags: ,
  • When a new workflow is published, does it overwrite the existing ones? If so, what happens to the workflow instances already in progress? Do the old active instances still use the old workflow or the new one?
  •  Does workflow foundation keeps track of revisions? If so, then do I have a choice of which revision to run? For example, from Jan - June, we used one process and after June, we started to use another modified process. Do I have a choice to indicate which process that I want to run? 
  •  Suppose after we deployed, we need to change the workflow for selected amount of cases,  say 2000 instances out of 15,000 base on certain requirements, can workflow do that?

Liquid Office 4.1 solved some of these issues, but during its development, I had several major issues.

Here are the Show Stoppers

1. Liquid Office 4.1 is asynchronous based.  When my web page makes a call through web services and ask the Liquid Offices to make a decision, update the database and alert the web page as to where to go next? Well, my page post-backed, and finished before Liquid Office finished its process. So my web page basically stayed where it is and never moved onto the places it was suppose to go. We "resolved" it by putting the thread to sleep for a good 5-10 seconds. As you can imagine, unbearable and unacceptable.

2. Liquid Office 4.1 does not support custom return types. All it returns is to let you know  if the workflow had exception or not. If no exception, then you have to assume it worked okay. There is no way of asking it to return the database surrogate key that was just inserted.

3. Liquid Office's engine makes new revisions separate from the old revisions. So if it is your intentions to replace the old revisions with the new revision (which is my case), sorry sir. No can't do. The old revisions are cached and the old workflow would still follow the old workflow. Not the new one.


.NET Workflow Foundation

I'm glad to find out that unlike Liquid Office 4.1, .NET workflow Foundation does not suffers the same type of issues.  Here are the answers replied via Microsoft MSFT

1. When a new workflow is published, does it overwrite the existing ones? If so, what happens to the workflow instances already in progress? Do the old active instances still use the old workflow or the new one?


No, it does not overwrite the existing ones. Workflows are .NET types and we use normal .NET versioning so if you make a change to a workflow you need to increment the version and make the assemblies for all running versions accessible to the workflow runtime. Old running instance will continue to run as long as the runtime has access to the assembly that they are defined in.

2. Related to #1.  . Does workflow foundation keeps track of revisions? If so, then do I have a choice of which revision to run? For example, from Jan - June, we used one process and after June, we started to use another modified process. Do I have a choice to indicate which process that I want to run?

Since it is a .NET type you just need to use the type from the version of the assembly that you want to run at the time.

3. Calling Workflow from ASP.NET, does it run asynchronously or synchronously? Liquid Office runs asynchronously and when its workflow is updating our database, the ASP.NET pages is already finished and such still display the old data. We had to refresh the browser a few times till workflow is finished updating the database. Show stopper.

It runs synchronously in that it is recommended to use the ManualWorkflowSchedulerService in ASP.NET and you must donate a thread for the workflow to be run. Once the current work is done the control is returned to the page.

4. Suppose after we deployed, we need to change the workflow for selected amount of cases,  say 2000 instances out of 15,000 base on certain requirement, can workflow do that?

You would want to use Dynamic Update to update the existing workflow and you can use any logic you would like to determine which instance you want to update and which you don’t, there isn’t a way to update all running instances in one shot.


Hopefully, I'm not too wordy with my article. I hope it gives those of you who are interested in .NET workflow foundation a basic understanding of what it does.

 
Anonymous comments are disabled