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

10/13/2011 6:20:47 PM #
Thanks for your infor
11/18/2011 2:42:00 AM #
<p><a href="http://www.cheap-moncler-coats.com/">Cheap Moncler Coats Kids</a> MR SNIGGS, the Junior Dean, and Mr Postlethwaite,<p><a href="www.cheap-moncler-coats.com/">Moncler Coats Women</a> the Domestic Bursar, sat alone in Mr Sniggs' room overlooking the garden quad at Scone College. <p><a href="www.cheap-moncler-coats.com/">Moncler Coats Men</a> From the rooms of Sir Alastair Digby Vane Trumpington, <p><a href="www.cheap-moncler-coats.com/">Fashion Moncler Jackets</a> two staircases away, came a confused roaring and breaking of glass.<p><a href="www.cheap-moncler-coats.com/">Fashion Moncler Jackets</a> They alone of the senior members of Scone were at home that evening <p><a href="http://www.brand-dress.com/">Moncler Sweater Men</a>.
11/25/2011 6:36:46 PM #
Very nice information <a href="www.nffleece.com/north-face-fleece-jacketsm-c-3.html"><strong>mens north face denali jacket</strong></a>. Keep sharing.<a href="www.nffleece.com/north-face-fleece-jacketsm-c-3.html"><strong>mens north face denali</strong></a>,<a href="www.nffleece.com/north-face-fleece-jacketsm-c-3.html"><strong>north face denali mens</strong></a>.I like it very much  and I will read more.<a href="www.nffleece.com/north-face-fleece-jacketsm-c-3.html"><strong>north face denali jacket mens</strong></a>.<a href="www.nffleece.com/north-face-fleece-jacketsm-c-3.html"><strong>mens north face denali jacket sale</strong></a>.
11/25/2011 8:34:25 PM #
<a href="www.thenikefreerun.net/womens-nike-free-run-2-c-3.html"><strong>womens Nike Free run 2</strong></a>.Wishing you the best of luck for all your blogging efforts.<a href="www.thenikefreerun.net/womens-nike-free-run-2-c-3.html"><strong>Nike Free run 2 for women</strong></a>,<a href="www.thenikefreerun.net/womens-nike-free-run-2-c-3.html"><strong>new Nike Free run 2</strong></a>.It’s good to see this information in your post.<a href="www.thenikefreerun.net/womens-nike-free-run-2-c-3.html"><strong>Nike Free run 2 sale</strong></a>,<a href="www.thenikefreerun.net/womens-nike-free-run-2-c-3.html"><strong>cheap Nike Free run 2</strong></a>.
12/8/2011 1:39:06 AM #
<p><a href="http://www.womens-ugg-boots.org/">Womens Ugg Boots</a> 'Five per cent of ninety pounds is <p><a href="http://www.cheap-ugg-boot.net/">Cheap Ugg Boot</a> four pounds ten shillings,' <p><a href="http://www.cheap-ugg-boot.net/">Discount Ugg Boots</a> said Mr Levy cheerfully.<p><a href="http://www.womens-ugg-boots.org/">Discount Uggs Boots</a> 'You can pay now or on receipt of your first term's salary. <p><a href="http://www.womens-ugg-boots.org/"> Ugg Outlet</a> If you pay now there is a reduction of 15 per cent.<p><a href="http://www.womens-ugg-boots.org/">Cheap Uggs Boots On Sale</a> That would be three pounds six shillings and sixpence.''I'll pay you when I get my wages,' <p><a href="http://www.cheap-ugg-boot.net/"> Womens Winter Boots</a> said Paul.'Just as you please,' <p><a href="http://www.cheap-ugg-boot.net/">Sheepskin Boots On Sale</a> said Mr Levy. 'Only too <p><a href="http://www.brand-dress.com/">Brand Dress</a> glad to have been of use to you.'
1/17/2012 12:14:06 AM #
I am glad to visit your blog.. I found this post is really useful one.. I love reading this kind of stuff.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading