Sequential Workflow - Part 2
partial class ATMSimulator
{
#region Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
[System.Diagnostics.DebuggerNonUserCode]
private void InitializeComponent()
{
this.CanModifyActivities = true;
System.Workflow.Activities.CodeCondition codecondition1 = new System.Workflow.Activities.CodeCondition();
this.DispatchAmount = new System.Workflow.Activities.CodeActivity();
this.GatherAmountInput = new System.Workflow.Activities.CodeActivity();
this.CashWithdrawalSequence = new System.Workflow.Activities.SequenceActivity();
this.WhileUserNeedsATMService = new System.Workflow.Activities.WhileActivity();
//
// DispatchAmount
//
this.DispatchAmount.Name = "DispatchAmount";
this.DispatchAmount.ExecuteCode += new System.EventHandler(this.DispatchAmount_ExecuteCode);
//
// GatherAmountInput
//
this.GatherAmountInput.Name = "GatherAmountInput";
this.GatherAmountInput.ExecuteCode += new System.EventHandler(this.GatherAmountInput_ExecuteCode);
//
// CashWithdrawalSequence
//
this.CashWithdrawalSequence.Activities.Add(this.GatherAmountInput); 3
this.CashWithdrawalSequence.Activities.Add(this.DispatchAmount);
this.CashWithdrawalSequence.Name = "CashWithdrawalSequence";
//
// WhileUserNeedsATMService
//
this.WhileUserNeedsATMService.Activities.Add(this.CashWithdrawalSequence);2
codecondition1.Condition += new System.EventHandler ( his.CheckUserNeedsATMService);
this.WhileUserNeedsATMService.Condition = codecondition1; 4
this.WhileUserNeedsATMService.Name = "WhileUserNeedsATMService";
//
// ATMSimulator
//
this.Activities.Add(this.WhileUserNeedsATMService);
this.Name = "ATMSimulator";
this.CanModifyActivities = false;
}
#endregion
private CodeActivity DispatchAmount;
private SequenceActivity CashWithdrawalSequence;
private WhileActivity WhileUserNeedsATMService; 1
private CodeActivity GatherAmountInput;
}
The major differnce from the Part 1 in this workflow is the inclusion of WhileActivity 1 so that the user can make more than one transaction in one go. WhileActivity is a CompositeActivity so it can conatin other activities 2. Since WhileActivity can execute only single activity, the previous activities are packaged in to a single SequenceActivity 3 and given to the WhileActivity 2. The Condition property of WhileActivity, which is set to an instance of CodeCondition 4 in this code, determines whether the WhileActivity should proceed or halt.
public sealed partial class ATMSimulator : SequentialWorkflowActivity
{
private int amountToWithdraw = 0;
private bool userNeedsService = true;
public ATMSimulator()
{
InitializeComponent();
}
private void GatherAmountInput_ExecuteCode(object sender, EventArgs e) 1
{
Console.WriteLine("please sepecify the amount to withdraw");
amountToWithdraw = int.Parse(Console.ReadLine());
}
private void DispatchAmount_ExecuteCode(object sender, EventArgs e) 2
{
Console.WriteLine(String.Format("Please collect {0:c} from the ATM", amountToWithdraw));
Console.WriteLine("Enter Q to quit");
userNeedsService = !Console.ReadLine().Equals("Q", StringComparison.OrdinalIgnoreCase); 3
}
private void CheckUserNeedsATMService(object sender, ConditionalEventArgs e) 4
{
e.Result = userNeedsService; 5
}
}
The sequence activities are executed 1 2 in the first go and sets whether the user needs to do further transactions 3. Based on the user's preference, CodeCondition event handler 4 sets the Result to a boolean value to signal whether the while loop should halt or proceed 5.
0 Comments