6.8. Flowchart

Flowchart is a new type of workflow that makes it easier to model certain types of problems, particularly those that return back to previous activities. Prior to WF4, this could be achieved by using a while loop, but flowchart workflows offer a more intuitive approach. We will create a flowchart workflow to simulate a customer's age being checked.

  1. Open Visual Studio and create a new workflow console application called Chapter6.Flowchart.

  2. Drag a Flowchart activity from the Flowchart group onto the designer surface. You should see something similar to Figure 6-13.

    Figure 6.13. New flowchart workflow
  3. The design view for flowcharts looks slightly different from sequential workflows. The green circle indicates where the workflow starts. We need to create a new activity to read input from the user. Create a new class called ReadInput.

  4. Enter the following using statement:

    using System.Activities;

  5. Now enter the following code:

    public class ReadInput : CodeActivity<Int32>
    {
        protected override Int32 Execute(CodeActivityContext context)
        {
            return Convert.ToInt32(Console.ReadLine());
        }
    }

  6. Save the class and compile the application.

  7. Open Workflow1.xaml.

  8. Drag a WriteLine activity beneath the green circle, change the display name to "What is your age?" and set Text to "What is your age?"

  9. Drag the new ReadInput activity beneath the "What is your age?" activity and change the display name to "Read input."

  10. Create a new variable called age of type Int32.

  11. On the ReadInput activity, set the Result property to age.

The next thing to do is determine if the customer is old enough to see the film (which in this case will always have an 18 rating). Flowchart workflows have a new type of activity not found in sequential workflows, called FlowDecision.

  1. Drag a FlowDecision activity beneath the ReadInput block and change the condition to Age >= 18. There are obviously two possibilities to this expression:

    • The customer is old enough, so they can see the film (FlowDecision condition = true).

    • The customer is too young, so shouldn't be seeing any movies (FlowDecision condition = false).

  2. To simulate the customer failing age verification, drag a WriteLine activity to the right of the flow decision and change the display name and text to "Sorry not old enough."

  3. Drag another WriteLine activity beneath the flow decision and change the display name and text to "Age validation successful."

  4. We now need to link up the activities we have just created. Move the mouse over the green circle that indicates the start of the flowchart workflow, and three gray dots will appear around it. Click the one on the bottom of the circle and then drag the mouse down to the ReadInput activity.

  5. When you near the WriteLine activity, three gray dots will appear around it. Drag the line to one of these dots and then release the mouse button to link up the start of the workflow with our ReadInput activity.

  6. Link up the "What is your age?" and ReadInput activities.

  7. We need to join the FlowDecision to the workflow. FlowDecision activities have two nodes, true and false, that surprisingly indicate the path to take when the condition specified is true or false. Drag the false node to the "Sorry not old enough" WriteLine activity and then drag another line from "Sorry not old enough" back to the ReadInput activity.

  8. Drag the true node on the FlowDecision activity to the "Age validation successful" activity.

  9. Finally, drag a line between the "What is your age?" and ReadInput activities. Your final workflow should look like Figure 6-14.

  10. Open Program.cs and add Console.ReadKey(); beneath the invoke command so the application doesn't close immediately.

  11. That's it; your workflow is ready to run. Press F5 to run it.

  12. Try entering different ages and note that unless you enter at least 18, the workflow will write "Sorry not old enough."

Figure 6.14. Final age validation workflow

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

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