Share via


Authoring Activities in Code or XAML

With Windows Workflow Foundation (WF4) you can author activities in Code or XAML.  Each has advantages and disadvantages

Visibility

Advantage – XAML

You can author them in the Workflow Designer.  Here is a activity I’ve been working with today.  You can easily see what this activity does by looking at it in the designer.

image

Code Activities

It’s not as though code is inscrutable.  You can look at it and read this but it isn’t quite as easy.  Not to mention that you have to learn how to work with the Workflow object model in code.

 this.sequence = new Sequence
    {
        DisplayName = string.Format("{0} Sequence", this.DisplayName),
        Activities =
            {
                // Write a message
                new WriteLine { DisplayName = string.Format("{0} Start WriteLine", this.DisplayName), Text = this.StartTextVariable },
                new If
                    {
                        DisplayName = string.Format("{0} If Delay", this.DisplayName),
                        // If the timespan is not zero
                        Condition = new InArgument<bool>(ctx => this.DurationVariable.Get(ctx) != TimeSpan.Zero),
                        // Delay will cause idle
                        Then = new Sequence
                            {
                                DisplayName = string.Format("{0} Then Sequence", this.DisplayName),
                                Activities =
                                    {
                                        new WriteLine { Text = this.IdleTextVariable, DisplayName = string.Format("{0} WriteLine going idle", this.DisplayName) },
                                        new Delay { Duration = this.DurationVariable }
                                    },
                            },
                        Else = new WriteLine { Text = this.IdleTextVariable, DisplayName = string.Format("{0} WriteLine No Delay", this.DisplayName) }
                    },
                new WriteLine { DisplayName = string.Format("{0} End WriteLine", this.DisplayName), Text = this.EndTextVariable },
            }
    };

Performance

Advantage – Code

Because there is no deserialization from XAML, there is a slight performance advantage to code based activities.  The amount of difference you will see depends on how many iterations of a test you run.  If I create the activities and invoke them in a loop 10 times the performance difference is about 20% faster with code.

However if I run them in a loop 10,000 times the performance difference is much less in fact it is so small as to be barely noticeable.

Of course, you must remember to cache your activities.

Which should you use?

Most people will use XAML activities because they like working with the activity designer.  If you are authoring activities for others to use, it is difficult to know how much of a penalty the XAML serialization will be.  In this case it probably makes sense to author your activities in code. All of the activities we ship are authored in code for this reason.

Happy Coding!

Ron Jacobs

https://blogs.msdn.com/rjacobs

Twitter: @ronljacobs https://twitter.com/ronljacobs