20 Aug 2009

WF 4.0 - Part 1. The Sequential Designer

UPDATE – Now Beta 1 Friendly :)

This is the first in a number of articles that look at the new features in the WF 4.0, and as such the features might change or be missing in futures releases.

Windows Workflow Foundation 4.0 (WF), along with .NET 4.0 was announced at PDC 2008 and those present were lucky enough to get their hands on a VPC image. The Beta 1 VS2010 ISO is now available for download, you can get it from HERE.

So what's all the fuss about? Well put simply, WF as we know it has been given a complete overhaul from the ground up! They're rewritten the entire foundation and simplified it no end. WF is now far simpler to use and easier to extend. So in the coming series of articles I'll walk you through the new features showing how they differ to the 3.x version.

We're start off with the new Designers provided with WF 4.0. I can't show you the State Machine Designer, as it has been dropped from the 4.0 release. Instead we’ll be getting the Sequential Designer, and the new Flowchart Designer which is kind of a Sequential/State Machine hybrid.

Workflow Designers

The first thing you notice with the designers is that they are now written in WPF! This gives a whole host of advantages to the Designer experience, first off you get all the standard benefits of a WPF application (zooming, scaling, etc) but more importantly it is far more responsive that the old 3.x designer. This has a knock on effect on how you can use Activities on the designer, but we'll come onto that later. Lets have a look at the Sequential Designer.

The designer hasn't changed much from its transition to 3.x, you still insert activities in a vertical line onto it's surface. But what has changed are the addition of a couple of very small innocuous buttons in the bottom left of the designer called "Arguments" and "Variables".

Arguments are the method into which you pass values from the outside world into the Workflow you'll be executing, or from a Workflow into an Activity. Previously in 3.x to pass Arguments into a Workflow this meant dropping into code and adding a number of Dependency Properties in by hand.

So how do you add Arguments to a Workflow in 4.0? Easy, you click the Arguments button and add each Argument into the dialog that appears.

Give the Argument a name, select a Direction, a Type and if required a Default value. Once complete, you’ve created your first Argument.

As you can see, we have the choice of assigning a direction to the Arguments. This does exactly as you'd expect, so values are either taken In (like passing by reference), In Out (like passing by Value), Out (return a value) or as a Property.

But how do you actually get the Arguments into a Workflow? This hasn't changed at all, you still pass in a Dictionary with string keys for each of the values. The key relates to the name of the Argument it will be assigned to.

The Variables functionality is new in WF 4.0 and designer to solve a problem of complexity from 3.x. In 3.x you would have activities binding to each other, but this could lead to some very odd behaviour if the activities bound to the wrong activity and make things complex as an Activity could bind to another activity from either end of a Workflow.

So what the WF team introduced in 4.0 is the concept of variables within a Workflow. Instead of binding Activities to other Activities we now have a broker in the middle to hold the value. It sounds terrible and a bit like a global variable, but it does work very well and is only scoped to the parent Workflow or child Activity, so it's a lot more like Members in a Class. You could almost go as far to say the Workflow is now stateful, as it contains values that change as it executes.

So what does the Variables dialog look like?

To create a new Variable supply a Name, Type and if needed a default Value.

Ok, so what about when we change the context of the Workflow designer? What happens then? Well in that case the Variables window will show all of the Variables available to the selected Activity from the parent Activity (or Workflow) and above. Here you can see that the child Sequence Activity has 2 Variables available to it. AVariable owned by the parent Sequence Activity and it’s own AScopedVariable.

An important point to note here is the mechanism in which we bind Variables to Activities. Binding is now done via an Expression mechanism, so when you bind you can either bind straight to a Variable or alternatively you can enter a value in the box. So say I want to display "Hello" in the Write Activity instead of creating a variable to hold "Hello" and binding to that, I can put "Hello" with quotes straight into the Text property. This is because the property takes in an expression that can either be an explicit value or some sort of expression that can be a value assignment, or property call.

Create your first Sequential Workflow

The following is a textual version of the screencast.

Ok, so lets build a Sequential Workflow using these new features in a Console Application. This Sequential Workflow is going to greet a person we name. Now I'm going to be using a number of custom activities to perform the work, but I won't be going into how they work as this will be covered in a future article.

The process I'll be following is quite simple, the User will be welcomed and then prompted for their name and a greeting will then be displayed.

The first thing we need to do with our Workflow is to add in the Activities, so I'll add in a single WriteLine Activity that write text out to a Console, a custom ReadLine Activity that reads in from the Console, the Greeting activity. I do this in exactly the same manner as we add Activities currently, by dragging them onto the surface in the order I desire.

Now I'm going to need 1 variable for this activity. To hold the name input by the User. I click on the Variables button and add in 1 variable of type string called Name.

I can now assign the Name variable to the Read Activity I'm using to take in the input from the User. I do this by going to the Properties pane with the focus on the desired Activity and binding it to the relevant Variable.

Now I need to bind the input of the Greetings Activity to the Name Variable, I do this in the same manner as before.


If I now build the solution and run it. I get the following experience.

So lets have a look at the code it generated:

This is another important distinction in WF 4.0, there has been a move away from code (VB or C#) and towards declarative coding using XAML. As a rule, the only time you will ever write VB or C# is when you are writing you're very own custom Activities, the rest of the time you will be generating XAML. (Note :- The line that is cut off is just the path to the XAML file).

In the next article, I'll be looking at the new Flowchart Workflow and Designer.

13 Aug 2009

Slides for the Intro to WF 4.0 talk

I’ve uploaded the slides for my Windows Workflow Foundation 4.0 to my SkyDrive, you can get at them via this link.

Now includes code for the 5 basic demos.  Enjoy the coding goodness!

11 Aug 2009

Upcoming events at NxtGenUG Southampton

Over the next couple of months we will be continuing our Web technologies theme by having Ben Hall down to talk about Testing ASP.Net applications on the 20th August. We’ve also managed to grab some uber swag from TypeMock and some of Roy Osherove excellent “The Art of Unit Testing” eBooks to give away! You can register here.

Then on 17th September we’ve got Yossi Dahan stopping by Southampton again, no BizTalk this time, but the new Windows Identity Framework! The Windows Identity Framework is Microsoft's offering for dealing with federated security. You can register here.

Then on 22nd October for our 2nd Birthday(!) we’ve hosting the follow up grudge match between Phil Winstanley and Sebastien Lambla as they duke out the merits of ASP.NET WebForms against ASP.NET MVC (and OpenRasta!). This was a very highly rated session in London, and promises to be very entertaining and informative. You can register here. More details to follow for this session on the site soon!

5 Aug 2009

Twitter on Windows Mobile 6

Over the last month or two I’ve been looking for a Twitter client for my HTC Touch Pro, as I wasn’t too keen on the mobile web version. I asked for recommendations on Twitter and the majority of people recommended either Pocketwit or Twikini.

I downloaded both clients so I could test them out simultaneously on the device during my day to day activities. All pictures taken from the relevant twitter client site. So here are my findings:

Pocketwit

Pocketwit had the more flash of the two interfaces, allowing the user to scroll up and down the timeline using via the phones touch screen. The tweets were in a nice big font with the person avatar to the right hand side.

Navigation was performed by dragging the tweets either left for context specific options on a tweet or right for a main menu like set of options. Whilst this looked excellent I found the binding to selected tweet was a little dodgy as sometimes the context menu wasn’t for the currently selected tweet.

I also had problems swapping from vertical to horizontal screen orientation when I opened the keyboard. The screen would get stuck in the previous orientation which meant options and navigation became a bit fiddly.

Twikini

Twikini has a more classic windows feels to it and doesn’t take advantage of the touch screen for navigation. Friends, Mentions and Direct Message are selectable from a menu option which then loads the appropriate timeline.

The tweets themselves are not always displayed in full, so you do have to open a tweet from time to time to read it in its completeness. Each tweet has the users avatar to the right of their tweet, and the text is in a very readable font.

During my use of Twikini I didn’t encounter any of the selection and orientation problems I had with Pocketwit, which has lead me to stump up the huge charge of $5 to get a full license :) A couple of weeks on, and I’m still happy with the client.

If I were to change anything about Twikini I would include some sort of status bar on it so I can see any new mentions or direct message, as currently I have to open those timelines up explicitly to see if any have arrived.

Conclusion

Both are cracking clients, but for the time being I’ll stick with Twikini, even though Pocketwit is free. Once Pocketwit gets out of Beta I’ll be revisiting it to see if they’ve managed to sort out the bugs that were enough for me to opt for Twikini.