Argument, Variables, Assign and Let Activity

Forum posts give me a lot of crazy ideas. This one might be a throwaway, but it’s the process of trying things out and exploring that triggers off yet more ideas. 🙂

Occasionally Workflow Foundation 4 developers feel bugged by the need for Variables. Or actually the ‘ceremony’ of using Variables for glue or piping between the input of Activity A and the output of Activity B.

Here are a few reasons I can think of that it’s no fun:

  • You are forced to invent names for incidentals. Wasted mental effort.

  • You can’t see the names so you will soon forget them and how you spelled them. Variables are invisible when the variables window is closed and once out of sight, out of mind.

  • Copy and paste requires much waving of the mouse.

  • If you break the rule and use your variable before declaring it then intellisense becomes your enemy rather than your friend.

Well save some of those thoughts for another day but thinking about all of that reminded me of a particular activity I don’t really love: the Assign activity.

Here’s a scenario: I want to introduce some intermediate value half-way through my workflow. Area = Pi * R * R. What steps might I need to do? I need to use the Assign activity. Steps:

  • Decide on a scope to create the variable at. Often the innermost scope is fine because I will use the value soon afterwards.

  • Right-click to create a new variable ‘Area’.

  • Change the type of the variable to double (since default is string).

  • Drag in an assign activity

  • Type ‘Area’ in the left-hand side (I feel compelled here to wait 2 seconds for intellisense to verify I typed it OK).

  • Type the expression in the right-hand side.

OK, so I’ve created my variable. Which is invisible. The Assign activity which initializes it, on the other hand, is extremely visible, and in fact it takes up oodles of space.


OK, the pet peeve have been unleashed. See how she runs!

Reimagining Assign

Where does that wasted space come from?

  • A title bar to proudly display ‘Assign’ or whatever name I care to give. I really do not care to give a name. I already named the variable, how many names do you want from me? You cannot get blood from a stone!

  • Sequence separators. Not much we can do about that, you might think! I mean even if we got rid of the assign we’d still have one of them at least…

And worse, there are a bunch of wasted actions too: naming something twice when once should really be enough, having to declare a type when it could be inferred like the var statement in C#, and the right-clicking and/or showing/hiding of the Variables gadget. So, let’s invent the Let activity. You can call it the Var activity though, if you like. Here’s the concept:

Let Activity

image Look! Less oodles!

OK… At this point, you can download the code if you like. I’ll warn you it’s not perfect, there are a couple kinks there. Bugs are welcome as comments but no promises to fix them. 🙂

A few notes on what you would see in the code.

Morphing: Using the MorphHelper class, the Variable contained is morphed between Variable<object> and Variable<T> for whatever type of expression you add on the right-hand-side.

ExpressionTextBox: The dude on the left is a textbox, bound to a variable Name property. The dude on the right is a standard ETB.

Overriding NativeActivity.CacheMetadata(): not perhaps actually necessary, but theoretically a good idea for perf reasons.

At the moment there’s an IActivityTemplateFactory called LetFactory which you should use for dragging it into the workflow. Having that show up in the toolbox right next to Let is something I’m not super happy with, but it works for now…

Let me know what you think.

Comments (2)

  1. Notre says:

    Hi Tim,

    I found a couple of things particularly interesting:

    – the code that was in the UpdateExpression method

    – the fact that the Let activity’s variable didn’t appear in the variable editor

    I’m not entirely clear on what the MorphHelper gives us, particularly the MorphProperties method.  Could you explain what it does, in the context of this example?

    Does WrapAsModelItem update the model item tree, or is a model item just created into thin air, not attached to the model item tree?

    I’m of two minds about the Let activity’s variable not showing up in the variable editor.  It’s nice in that it doesn’t ‘clutter’ the variables editor, and the various productivity items you listed in the post. It’s "bad" in that I can’t discover all variables in scope, by looking in the variable editor.

    Overall, an interesting idea.



  2. tilovell09 says:

    Yes, WrapAsModelItem() creates the model item in ‘thin air’.

Skip to main content