6.5. Hello WF4

In this section, we will write a simple workflow that will handle a booking for a movie theater. Our example workflow will do the following:

  • Process a booking for a specific film.

  • Check if seating is available for the requested showing. In our example, to keep everything simple, if less than five tickets are requested, then the booking will succeed.

  • Return a Boolean value indicating whether the booking was successful and a hard-coded alphanumeric ticket reference (blank if unsuccessful).

WF4 is a radical change, so even if you are an experienced WF developer, please take the time to work your way through this example, as the designer has changed completely and a number of new concepts have been introduced.

Open Visual Studio and create a new workflow console application called Chapter6.HelloWF. You should now see a screen similar Figure 6-5.

Figure 6.5. New sequential workflow

The first change that you will notice is that the WF designer, like the IDE, is rewritten in WPF and is much quicker than the previous version.

We are going to be creating a sequential workflow first, so open the toolbox and drag a Sequence activity from the Control Flow group to the designer.

Next, we will need to supply our workflow with information about the customer's booking. In WF4, arguments and variables are used pass data between activities. This is much easier than in WF3 where dependency properties had to be used.

6.5.1. Arguments and Variables

Arguments and variables are unique to each running instance of a workflow and hold and return values. (Technically, arguments and variables don't actually hold the values, but instead tell the individual workflow how to access the values.) They can be of any type, and arguments can be used as inputs, outputs, or both input and outputs.

The visibility or scope of a variable is dependent on where it is declared. So if a variable is declared inside an activity, it will be visible to child activities, but not to parent activities. As per traditional coding practices, you should minimize the exposure of your variables and arguments.

6.5.1.1. Creating an Argument

Let's create a new argument (no, not the kind you have with your wife when she is trying to get you off the computer when you are trying to write a book), but a WF4 argument.

  1. On the gray bar at the base of the designer window is an Arguments button. Click this. A new section will open in the designer (Figure 6-6).

  2. Click where it says Create Argument and add three new arguments with the following settings:

    • FilmName (type String, direction In).

    • ShowingDate (type String, direction In). In reality, we would pass in a FilmShowingID variable, but this reduces typing of pointless demo code.

    • NumberOfTickets (type Int32, direction In).

      Figure 6.6. Creating new arguments
  3. We will also need two variables to hold whether the booking was successful and the booking reference to return to the user. Click the Variables button at the base of the screen and create two new variables (Figure 6-7):

    • BookingReference (type String; set the default to "")

    • BookingSuccessful (type Boolean)

      Figure 6.7. Creating arguments for our workflow
6.5.1.2. WriteLine

WriteLine is an built-in activity that allows us to write output to the console window or a text writer. We will use it to write a message to the console window to inform us that the workflow has started.

  1. Drag a WriteLine activity from the toolbox (Primitives section) onto the Sequence activity, placing it on the gray arrow in the white box.

  2. Click the WriteLine activity, and then in the Properties window, change Display Name to "Workflow started." Display Name is a property present on most activities, and is simply a label for the activity. Display Name is also used for constructing a breadcrumb trail in the IDE (discussed later when we drill down into child activities), so it is good practice to set it to something useful. Finally, change the text property to "Workflow started" (include the quotes).

    NOTE

    When writing out values in expressions, WF uses VB.NET expression syntax, and requires you to contain values such as text properties within quotes. I use quotes in this tutorial to make the text easier to pick out, but you should not enter the quote characters when setting properties such as Display Name.

  3. Click the Sequence activity and change the display name to "Book Film." Your workflow should be looking like Figure 6-8.

    Figure 6.8. Creating our workflow
  4. We want to see the output of our workflow before it closes, so open Program.cs and add the following code at the end of the Main() method:

    Console.ReadLine();

  5. Press F5 to run your workflow, and you should see "Workflow started" outputted to the screen. Congratulations! You have created your first workflow application.

6.5.1.3. Creating Another Sequence Activity

The next step in our contrived example is to check if seating is available for a specific film showing. We will create a Sequence activity that contains a number of other activities to perform this check.

  1. Drag a Sequence activity below the WriteLine activity.

  2. Change the display name to "Check availability."

  3. To drill down into an individual activity, you double-click it. So double-click the "Check availability" sequential activity you just created, and you should see a screen similar to Figure 6-9.

    Figure 6.9. Drilled-down view of Sequence activity

As we are "inside" one of the activities, you probably want to know how to get back to the level above. There are two ways of doing this:

  • Right-click the design surface and select the View Parent option.

  • Use the breadcrumb trail that is automatically constructed at the top of the page. Note how the activity's display name is used to form the breadcrumb trail. This is a very good reason to set it to something descriptive.

Individual activities can be collapsed and expanded to improve your view of the workflow.

6.5.1.4. Checking the Number of Tickets with an If Activity

We now need to check if the number of tickets requested is more than five, and in our example we will prevent such a booking.

  1. Drag an If activity onto the designer (in the "Check availability" activity).

  2. Change the display name to "If NumberOfTickets>5."

  3. Double click the If activity.

  4. In the Properties window, select the Condition property and click the ellipsis button.

  5. Enter the following in the Expression Editor window:

    NumberOfTickets > 5

Expressions are written in VB syntax. Expressions must be entered in VB syntax whether you are creating a VB or C# workflow project. The WF team claims that VB is more intuitive (ahem) and that some power users will be used to VBA syntax from a product such as Excel.


6.5.1.5. Unsuccessful Booking and the Assign Activity

The Assign activity allows us to assign values to variables. We will use it to update the BookingSuccessful variable.

  1. Drag an Assign activity from the Primitives group into the Then box of the If activity.

  2. Change the display name to "Booking unsuccessful."

  3. Change the To property to the BookingSuccessful variable.

  4. Change the Value property to False.

6.5.1.6. Successful Booking and the Parallel Activity

We want to set the BookingSuccessful property to True and also set a booking reference. The Parallel activity allows us to perform two activities at the same time (OK, this is unnecessary for this situation, but I wanted to make you aware of this cool feature).

  1. Drag a Parallel activity from the Control Flow group to the Else segment of the If activity.

  2. Change the display name to "Booking successful." Your If activity should look like Figure 6-10.

    Figure 6.10. If activity
  3. Double-click the "Booking successful" activity.

  4. Drag an Assign activity onto the designer, and change its display name to "Assign Booking successful."

  5. Change the To property to the BookingSuccessful variable.

  6. Change the Value property to True.

  7. Drag another Assign activity onto the arrows to the right of the original Assign activity, and change the display name to "Get booking ref."

  8. Change the To property to BookingReference.

  9. Change the Value property to "abcde" (we will hard-code this value in our example; note that the quotes are needed). Your Parallel activity should look like Figure 6-11.

    Figure 6.11. Parallel activity allows you to specify that tasks should occur at the same time.
6.5.1.7. Displaying the Output of the Booking

We will now use another WriteLine activity to display the output of our workflow.

  1. Click Book Film in the breadcrumb trail to return to the top view.

  2. Drag another WriteLine activity to the end of the workflow and change the display name to "Display output."

  3. On the Text property, click the ellipsis and enter the following:

    "Booking result: " + BookingSuccessful.ToString() + " Reference: " + BookingReference.ToString()

6.5.1.8. Supplying Arguments to a Workflow

The only thing left to do is supply our workflow with some initial arguments. As per previous versions of WF, arguments are supplied as a dictionary variable.

Names are case sensitive and magic strings are bad, so in real-world applications you will probably want to create a wrapper class instead of using the following approach. A common approach is to inherit from the Dictionary class and create properties to set values within it.


  1. Open Program.cs and add the following using statement:

    using System.Collections.Generic;

  2. Modify the Main() method to the following to create and pass in a dictionary of our initial variables:

    static void Main(string[] args)
    {
        Dictionary<string, object> Arguments = new Dictionary<string, object>();
        Arguments.Add("FilmName", "Terminator");
        Arguments.Add("ShowingDate", System.DateTime.Now.ToString());
        Arguments.Add("NumberOfTickets", 4);
    
        WorkflowInvoker.Invoke(new Workflow1(), Arguments);
        Console.ReadLine();
    }

  3. That's it; you have just created your first workflow. Now press F5 to run it.

  4. Try changing NumberOfTickets to 5. Do you get a different result?

We covered a lot in this short example. You discovered the new workflow designer, how to use arguments and variables, and some of the new activities in WF4.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.158.116