Home > WF4

[WF4]Authoring WF4 using imperative code(II)

13. July 2010

Half a year ago, I demonstrated authoring workflow using imperative code . Today I want to talk some further more about authoring WF4 workflow using imperative code. The main focus of this article are two classes: Activity and DynamicActivity.

Activity

We can create a Sequence activity dynamically by the following code:

    public static Activity GetWF() {

        Variable<string> var =

            new Variable<string>("var", "hello workflow");

        return new Sequence {

            Variables = { var },

            Activities ={

                new WriteLine{Text="Workflow Started"},

                new WriteLine{Text=new InArgument<string>(var)},

                new WriteLine{Text="Workflow Ended"}

            }

        };

    }
    ///~Code list 1.

You may are wondering now that how can we add Arguments(InArguments,OutArguments and InOutArguments) to this Sequence just like we did in the workflow designer? Unfortunately, we cannot use Arguments in this way. Sequence is a pre-defined type. we can assign values to create a instance of Sequence. but we can not add Arguments to the pre-defined Sequence Type. The right way to define a workflow is inherited from the Activity class.

We can create a workflow with Arguments like this:

    public class MyCodeWorkflow : Activity {

       public InArgument<string> inMSG { get; set; }

       public OutArgument<string> outMSG { get; set; }

       public MyCodeWorkflow() {

           this.Implementation = () => new Sequence {

               Activities = {

                   new WriteLine{

                       Text=new InArgument<string>(

                           (activityContext)=>this.inMSG.Get(activityContext)

                       )

                   },

                   new Assign<string>{

                       To=new ArgumentReference<string>("outMSG"),

                       Value=new InArgument<string>(

                           (activityContext)=>this.inMSG.Get(activityContext)

                       )

                   }

               }

           };

       }

    }
    //host

    static void Main(string[] args) {

        IDictionary<string, object> input = new Dictionary<string, object>();

        input.Add("inMSG","hello");

        IDictionary<string, object> output = new Dictionary<string, object>();

        MyCodeWorkflow activity = new MyCodeWorkflow();

        output = WorkflowInvoker.Invoke(activity,input);

        Console.WriteLine(output["outMSG"]);       

    }
      ///~code list 2.

In the above code, we defined a workflow:MyWorkflow, and then run it with a parameter. The workflow is actually a c# type.

DynamicActivity

By implementing a class inherited from Activity we define a workflow using imperative code.Now, you may thinking that can we create a workflow intance just like creating a Sequence instance showed in code list 1? the answer is yes. we can use DynamicActivity to create a workflow instance dynamiclly using imperative code.

    static void Main(string[] args) {

        DynamicActivity dynamicWF = GetDynamicWF();

        Dictionary<string, object> parameters =

            new Dictionary<string, object>(){

                {"Text","Hello workflow"}

            };

        WorkflowInvoker.Invoke(dynamicWF, parameters);

    }

    public static DynamicActivity GetDynamicWF() {

        InArgument<string> Text = new InArgument<string>();

        return new DynamicActivity() {

            Properties = {

                new DynamicActivityProperty{

                    Name="Text",

                    Type=typeof(InArgument<string>),

                    Value=Text

                }

            },

            Implementation = () => new Sequence() {

                Activities = {

                    new WriteLine(){Text="Workflow Start."},

                    new WriteLine(){

                        Text=new InArgument<string>(

                            activityContext=>Text.Get(activityContext)

                        )},

                    new WriteLine(){Text="Workflow end."}

                }

            }

        };

    }   
  ///~code list 3

   In the above code, we can find out that it is no long needed to define a workflow before using it. we can create a workflow dynamiclly in the runtime.

WF4 , ,

Comments

7/19/2010 7:31:33 AM #
Hey... I don't know if you've been making changes, but your pages aren't displaying correctly for me. The borders of the text are overlapping. I didn't do this the last time I was here. I don't know if it's me or if you've made a change... Just thought you might want to look at it. Thanks! Kareem Miyasato
andrew zhu
andrew zhu
7/20/2010 1:37:50 AM #
What web browser are you using? I have test the page in IE8,firfox and Chrome
8/26/2010 8:10:49 AM #
Hello, I do not normally put up feedback on blogs, as I wish to learn only. However I find the article that you have written earlier has very insightful info, and I find it very informational. Anyway, I am questioning whether or not you might be open for hyperlink change, as I hope that we will agree on a mutual link trade agreement. Hope to listen to a positive reply from you, and have an incredible day!
8/26/2010 8:20:00 AM #
->" find the article that you have written earlier has very insightful info, and I find it very informational. Anyway, I am questioning whether or not you might be open for hyperlink change"
the original articles are still available here:
http://xhinker.com/archives.aspx
All links works.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading