I remember one of the projects I have designed the architecture for, it was 3 years back, this project encompassed some complex business processes that were made of more than 7 activities, firstly I started by splitting these activities into static, or shared, methods; these methods are taking the business object, and one more helping object that were holding the global status of the process, one of these processes were to place an order with details, and checking the system for the customer history to apply some business rules and giving the customer discounts, vouchers, etc.. , based on his history with the system, and then validating the customer's credit card locally to check whether it's adhering to the conventional credit card format, also I had a Fraud Management System, that checks for the history of the card by applying some business rules, and then the card gets out with one level of Fraud ordered by severity, if the card is not fraudulent, the data is submitted with Card information, Card holder information, amount to be deducted to an external system to check the validity of the card, deduct the amount, then the external system should send one acknowledgment to the sender system, indicating the success or failure of the process, and this acknowledgment is sent to a return URL, then the system should read the response and update the records.
Apparently this is one of the very basic patterns of using a workflow system, and it is painful to manage such a business process without a powerful workflow engine that provides you the tough stuff out of the box, I am sure that you may be asked to implement a similar process in your application, and basically you will need to implement this process in a windows application or web application, by all the means you have to have a powerful engine to do the work for you, at the time of implementing that project, I had to write too much code, to implement the process, and I have searched a lot to make that implementation powerful enough to satisfy the requirements, actually I tried to imitate the behavior of the Order pipeline in Microsoft Commerce Server.
Now .NET 3.0 does the magic, Thanks to Workflow Foundations, I am sure you've heard about it, but I just wanted to derive a real life example in which I suffered a lot to mimic a workflow engine behavior, in the rest of this post, I will address the major features of WF, and the problems it solves, and show how Microsoft is using the WF in her products, not surprisingly Microsoft has its own corporate tools that they use to develop their products, Microsoft had custom tools to verify that, which gave birth to what's called now WF, WF is a workflow engine that does the magic, it's part of .NET framework 3.0 that includes Windows Workflow Foundations, Windows Communication Foundations, Windows Presentation Foundations, and Windows Card Space, In this post I am just concerned about the WF, if you need more info about the other technologies, just Google it and you will get dozens of useful links, .NET 3.0 is coming by default with Windows Vista, but yet it's downloadable from Microsoft site, and to get your hands dirty with some walkthroughs and code, you will need to install the .NET 3.0 in case it's not there, and then you will need to download the VS.NET 2005 WF extensions; to be able to create WF projects directly from VS.NET 2005.
Let's get back to our first real life example, What do you think of such a process? I think it is a complex one, and based on the difficulties I have faced let me address you the problems of implementing such a process:
- Managing the transition from one activity to another:
- Business Rules Management:
- Transactional Support:
- Change Management and Modeling Business Process:
WF makes the life easier and more organized, well architected, so in the following points I will mention how WF solves these problems, respectively, out of the box:
- Simply Speaking WF is just a set of APIs that are shipped with Windows OS ( formerly known as WinFx ), once you install WF, you will find the major namespace is System.Workflow, and under this namespace you will have more than one namespace, actually whenever you start building your workflows you use a designer that is generating the code behind the scenes, so you can design your workflow right from the designer, and it's worth mentioning that in WF you have two types of Workflows, first is the Sequential Workflow, second is the State Machine Workflow, the difference between both is quite clear but sometimes it is not, however the sequential is just when you drag your activities one after another, and each is getting executed sequentially, but for the State Machine, you define a set of states that model your process, so in case of an Order you may have Started, Saved, Submitted, Paid, Shipped, ShippingConfirmed, Closed, and you can define all the logical transitions between the possible states, so for example you can make the order state is Closed if and only if the previous state is ShippingConfirmed, but for Submitted state, the previous state should be either Started or Submitted and so on, so when the order holds one activity some action has to be executed.
- WF has a built-in Rule Engine, so you can combine your activities with business rules or policies, let's say that if the user has purchased before with amount 1000 $, he will get 10% discount for all upcoming orders, if the purchase reaches 5000$, then he will get a 15% discount and so on, also this is very important for saving business facts or values, like percentages of discounts, maximum delivery period, service charges, etc..
- WF offers a great support for Transactions, and basically we have two kinds of transactions the first is the ACID (Atomicity, Consistency, Isolation, Durability), that's the model of ADO.NET and the second one is the Long-Running Transactions, most of developers are familiar with the first one, but the second one requires some attention, and this is the transactional model that I encountered in the example derived above, because the communication between the system and the Credit Card system was managed through the Internet, so let's say that after I submit the amount and card information to the credit card management system, and after the amount is debited from the customer's card, the order status update in the local system's database fails, if this occurs, there is no way to roll back an external system's operation, so here the need to what we call "Compensation" arises, and in this case we have to carry out another activity to roll back the activity that has been done before, so in this case the roll-back activity would be to "credit" the same amount into the customer's card, so in long-running transactions the developer is accountable to roll back the activities carried out before, actually WF offers great support for both ACID and Long-Running Transactions through the workflow designer, it's interesting to try some transactions by using transaction scopes available at the toolbox.
- Changes in WF are not that complex as I encountered before, for example the workflow logic itself is very easy to change through the workflow designer, also on the level of business rules, you can modify your rules without touching the code or recompiling it, modeling also is a real fun, using the workflow designer is very interesting and I consider it one of my latest hobbies, in future posts I will elaborate about how to create workflows and how to call it from windows / web applications.
Labels: Windows Workflow Foundations