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.
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.
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.
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.
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).
Click where it says Create Argument and add three new arguments with the following settings:
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):
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.
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.
Click the Sequence activity and change the display name to "Book Film." Your workflow should be looking like Figure 6-8.
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();
Press F5 to run your workflow, and you should see "Workflow started" outputted to the screen. Congratulations! You have created your first workflow application.
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.
Change the display name to "Check availability."
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.
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.
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.
Change the display name to "If NumberOfTickets>5."
Double click the If activity.
The Assign activity allows us to assign values to variables. We will use it to update the BookingSuccessful variable.
Change the display name to "Booking unsuccessful."
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).
Change the display name to "Booking successful." Your If activity should look like Figure 6-10.
Drag an Assign activity onto the designer, and change its display name to "Assign Booking successful."
Drag another Assign activity onto the arrows to the right of the original Assign activity, and change the display name to "Get booking ref."
Change the To property to BookingReference.
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.
We will now use another WriteLine activity to display the output of our workflow.
Click Book Film in the breadcrumb trail to return to the top view.
Drag another WriteLine activity to the end of the workflow and change the display name to "Display output."
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.
|
Open Program.cs and add the following using statement:
using System.Collections.Generic;
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(); }
That's it; you have just created your first workflow. Now press F5 to run it.
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.
3.133.158.116