Sunday, September 30, 2012

Windows Workflow Foundation

What is a workflow?

It is a framework that enables user to create system and process flow easily. Users can see controls that look like blocks and connectors in the UI design page, and you can simply drag and drop these blocks and connectors together to construct a flowchart, then a system will be created and functioning without the need to write lots of code. Finally, you can understand how the system work base on the flowchart.

Here is one sample that I had done while I was learning it.
I created a very simple workflow for leave application with the scenario of when I submit my leave application, it should automatically check for leave cannot be taken for more than 10 days, the application start date must be a future date, and the end date must be greater than start date.


Sample workflow


Things that we must know when we create workflow.

1. Variable - you can declare variables and use it in the workflow or inside a scope (block). It is useful for flagging purpose.

 
2. Arguments - you can determine the input or output parameters of a workflow


3. Imports - you can find the imported assembly namespaces here.



How to pass entity / object into workflow?

After you have created your own entity class, just simply add it as project reference to your workflow project. You actually pass and use your own object in workflow by setting the argument. But, there is another way to pass object into workflow, which is going through WCF via messaging way, but I have yet to discover how to do it. I will update and cover it in my next blog post.

So, go to the arguments of your workflow (as above screenshot), declare a name, set the direction, then set the argument type. While setting argument type, you are suppose to choose your entity object, but it will not be listed there. You just need to import it manually by selecting "Browse for Types...". Then select your entity project under <Referenced Assemblies>.



I have contructed my workflow, so how to test or debug?

Create a new unit test project. Then, add the workflow and entity project reference to your unit test project. Refer to following sample code to create an unit test.

        [TestMethod]
        public void ApplyLeave()
        {
            IDictionary input = new Dictionary();
            IDictionary output;

            Leave leave = new Leave();
            leave.Duration = 3;
            leave.StartDate = DateTime.Now.AddDays(1);
            leave.EndDate = DateTime.Now.AddDays(4);

            input.Add("LeaveApplication", leave);

            output = WorkflowInvoker.Invoke(new ApplyLeave(), input);

            Assert.AreEqual(((Leave)output["LeaveApplication"]).IsApproved, true);
        }

WorkflowInvoker is the class that you use to invoke your workflow. You can execute your workflow at any place such as ASP.net, WCF, windows form, console application, even unit test, and etc. The WorkflowInvoker accept and return Dictionary<string, object> type only. The dictionary key is the argument name, while the value is the argument value.

In my workflow design, I set my input argument type as my entity object type. Therefore, base on my above sample code, I actually construct my entity object, then keep it in my Dictionary<string, object> object instance, then pass it to the WorkflowInvoker. The passed in object will go through the workflow process and then return the final result back to WorkflowInvoker.

Summary

You should be able to notice that the idea of workflow actually is to make an object properties value change after one process end by another process. And, the value actually represent a status of a "thing".

By the way, you can create breakpoint in your workflow by right clicking at the block, then select Insert Breakpoint. So, you are able to identify the problem easily when you encounter any error or workflow does not work correctly while running the unit test.

You can find my sample code here: Download Here

 

5 comments:

  1. I downloaded your code but was not able to load due to incompatibility issue. could you please post step by step procedure. I want to create a .net web application with workflow. The procedure is user enters leave and it should be send to manager for approval and on his approval should be sent to HR.

    ReplyDelete
  2. Can You please Share your email or whatsaap contact no.

    ReplyDelete
  3. Please share the code my email kadarshah@gmail.com

    ReplyDelete
  4. hello, I am interested in your project. can you share the code. allen.ghadiri@gmail.com. thanks

    ReplyDelete

Send Transactional SMS with API

This post cover how to send transactional SMS using the Alibaba Cloud Short Message Service API. Transactional SMS usually come with One Tim...