After completing this chapter, you will learn:
1. Learn how to use ifelse activities to perform conditional expressions
2. Learn how to use while activities to perform loops
3. Understand how the Replicator activity simulates a for loop and how it is used.
We've seen how to execute code inside and outside the workflow, already know how to handle exceptions, pause processes, and terminate our workflows when things get out of control. But there is no doubt that the main components of any computer system should have the ability to make judgments based on run-time conditions to perform different tasks. In this chapter, we will demonstrate some of the workflow activities that require us to deal with if/else scenarios and basic loops.
Conditions and conditions of treatment
Now, you may not be surprised that you find WF provides an activity that logically processes control flow based on run-time conditions. After all, if WF provides an activity to throw and catch exceptions, why does it have no corresponding activity to test for the execution of the workflow and make decisions based on the results they get?
The activities that we will test in this chapter include IFELSE activities, while activities, and replicator activities. The role of the IfElse activity is to test a condition and execute a different workflow path based on the test results. A while activity is used to perform a while loop. For a For loop, it is done using the Replicator activity. Now start our study with the sample application in this chapter.
Note: In this chapter you will rely on codecondition to deal with the conditions, which (codecondition) means that you will write down the C # code to handle the conditional expression. In Chapter 12 ("Policies and Rules"), you will use RuleCondition to handle the value of the conditional expression, RuleCondition uses a rule-based approach to WF. Both methods are equally valid.
Qustioner applications
The sample application in this chapter is a Windows Form application that will ask you to answer three questions that you can modify. (The content of the problem is saved in the application's Settings property.) You can also specify whether these issues are independent or interrelated.
When the workflow starts executing, you want to pass these issues and related situations to the workflow. The interrelated questions will be raised only when the answers to the previous questions are correct. For example, if someone asks you: "Have you seen the documentation?" "If you don't, it doesn't make much sense to ask the following question:" Do you approve this document? "If the question is relevant, then the first answer is negative, it will return a negative answer, and the remaining questions will not be considered and return to the negative answer."
Separate questions require you to answer, regardless of what you answered in the previous question. For example, the question, "Do you like ice cream?" "and the question." Is it raining outside now? "is irrelevant. Whether you like ice cream or not, your answers are independent of the weather outside. For the question of independence, no matter whether you are affirmative or negative in the previous question, you will be further asked.
The user interface is shown in Figure 9-1. If you modify the contents of any of the three questions, the new issues will automatically be saved to your application's settings (the same type of problem). These questions produce "Yes/no" answers that allow workflows to pass these answers back to the host application as a Boolean array.
Figure 9-1 Questioner main application interface
When you click the Execute button, the questions are presented in the message box with the Yes and no buttons. Once the workflow has finished processing all of these problems, it returns a Boolean array to the host application. The host application examines the array to display a different user interface.
When the workflow executes, the answer results are displayed as a blue orb (Figure 9-1). When the workflow task is completed, the answer will appear in the form of a green sphere, and the unanswered answer will appear in the form of a red orb. If all the answers are passed, the "final answer" picture will be presented as a green orb. However, if any of the three questions do not pass, the "final answer" picture will be presented in the form of a sphere with a "8" word. As shown in Figure 9-2.
Figure 9-2 Questioner user interface during application execution
For you, the purpose of using this application is to test the three activities in this chapter. For the first iteration, questioner will use the IfElse activity to determine what action process to perform. These questions will still be asked in the second iteration, and we will use the while activity to ask questions. For the last iteration we will use the Replicator activity to simulate the for loop for questioning. For each iteration of the application, we will use the techniques demonstrated in the previous chapter to pass the results of the answers back to the host application.