Share via


Making a Withdrawal

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

Before making a withdrawal, click the Install Perf Counters button in the main QuickStart application window to install the performance counters used by the Performance Counter Handler. For details about how the QuickStart application installs and uninstalls performance counters, see "Installing and Removing Performance Counters" in The Performance Counter Handler.

To make a withdrawal, select a user in the Current User drop-down list, and then click Withdraw. In the Withdraw dialog box, enter the required withdrawal amount, and then click OK.

Note

If you do not install the performance counters that the Performance Counter Handler requires, you will see the error message "The requested Performance Counter is not a custom counter, it has to be initialized as ReadOnly" in the Exception text box on the main form when you click the Withdraw button.

The following extract shows the code that runs when you click the Withdraw button. It creates a new instance of the AmountEntryForm, specifying the value Withdraw from the AmountDialogType enumeration defined within that class, and shows the form. If the user clicks the OK button in the AmountEntryForm, the code clears any existing text from the Exception text box in the main form, and calls the Withdraw method of the BankAccount class. If an error occurs, the code places the message into the Exception text box.

private void withdrawButton_Click(object sender, EventArgs e)
{
  AmountEntryForm form = new AmountEntryForm(AmountDialogType.Withdraw);
  DialogResult result = form.ShowDialog(this);
  if (result == DialogResult.OK)
  {
    exceptionTextBox.Text = String.Empty;
    try
    {
      bankAccount.Withdraw(form.Amount);
    }
    catch (Exception ex)
    {
      exceptionTextBox.Text = ex.Message;
    }
  }
}
'Usage
Private Sub withdrawButton_Click(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles withdrawButton.Click
  Dim form As New AmountEntryForm(AmountDialogType.Withdraw)
  Dim result As DialogResult = Form.ShowDialog(Me)
  If result = DialogResult.OK Then
    exceptionTextBox.Text = String.Empty
    Try
      bankAccount.Withdraw(form.Amount)
    Catch ex As Exception
      exceptionTextBox.Text = ex.Message
    End Try
  End if
End Sub

The AmountEntryForm contains the declaration of the AmountDialogType enumeration, with the two values Deposit and Withdraw. The constructor of the AmountEntryForm class uses the value passed to it to set the window title and the prompt text to the appropriate values, allowing the application to use the same form for both deposits and withdrawals.

Policies for the Withdraw Method

The configuration of the QuickStart causes the Policy Injection Application Block to create a handler pipeline containing the Logging Handler, Authorization Handler, Performance Counter Handler, and Exception Handling Handler for the Withdraw method. In addition, the Withdraw method carries a ValidationCallHandler attribute that, in conjunction with attributes on the withdrawAmount parameter, adds a Validation Handler to the pipeline that specifies the value passed to the method must be greater than zero and less than or equal to 1,000.

[ValidationCallHandler]
public void Withdraw([RangeValidator(typeof(Decimal), "0.0",
                     RangeBoundaryType.Exclusive, "1000.0", RangeBoundaryType.Inclusive,
                     MessageTemplate="Withdrawal amount must be between zero and 1000.")]
                     decimal withdrawAmount)
{
  if (withdrawAmount > balance)
  {
    throw new ArithmeticException();
  }
  balance -= withdrawAmount; 
}
'Usage
<ValidationCallHandler()> _
Public Sub Withdraw(<RangeValidator(GetType(Decimal), "0.0", _
                    RangeBoundaryType.Exclusive, "1000.0", RangeBoundaryType.Inclusive, _
                    MessageTemplate:="Withdrawal amount must be between zero and 1000.")>  _
                    ByVal withdrawAmount As Decimal)
  If (withdrawAmount > balance) Then
    Throw New ArithmeticException()
  End If
  balance -= withdrawAmount 
End Sub

Note

For an explanation of the QuickStart configuration, see The Windows Forms QuickStart.

If the value entered is greater than zero and less than or equal to 1,000, and the currently selected user is Alice, the Withdraw method call will succeed. The Policy Injection Application Block executes the Validation Handler, the Logging Handler, the Authorization Handler, the Performance Counter Handler, and the Exception Handling Handler within the pipeline to the Withdraw method.

Effects of the Validation Handler

If you enter a value of zero or a negative value in the text box in the AmountEntryForm, the Validation Handler added into the pipeline by the ValidationCallHandler attribute on the Withdraw method prevents invocation of the method and the Exception text box on the main form displays the message, "Parameter validation failed, Parameter name: withdrawAmount."

Effects of the Logging Handler

The Logging Handler appends two entries to the application's audit log file; one entry is appended before the Withdraw method executes and one entry is appended after it completes. With the exception of the method and parameter names, these entries are the same as those shown for the Deposit method in the topic Making a Deposit. You can click the View Log File button to see the entire contents of the application's audit log file.

Effects of the Authorization Handler

If the currently selected user is not Alice, the Authorization Handler will prevent the Withdraw method from executing, because the rules in the Security Application Block configuration authorize only Alice (as a Teller) to execute the Withdraw method. In this case, the Exception text box on the main form shows the message, "Authorization failed".

Effects of the Performance Counter Handler

If you have installed the performance counters using the Install PerfCounters button in the main QuickStart application window, the Performance Counter Handler updates the five performance counter instances defined in the application's configuration each time you execute the Withdraw method.

To see the performance counters

  1. In the main QuickStart application window, click Open Performance Monitor.
  2. Remove the existing default counters. To do this, select each one in the list below the performance chart, and then press the DELETE key or click the X button on the toolbar.
  3. Press CTRL+I or click the + button on the toolbar to open the Add Counters dialog box.
  4. In the Performance object drop-down list, select Enterprise Library Policy Injection, and then select the All counters option button below it.
  5. Click the Add button and then click Close to return to the main Performance window.

As you execute the Withdraw method, you will see the performance counter values change to reflect this, as shown in Figure 1.

Ff647561.43d68d76-8142-45f5-9891-8fe12cbbcf8a(en-us,PandP.10).png

Figure 1
The performance counters generated by the Policy Injection QuickStart

Effects of the Exception Handling Handler

If you attempt to withdraw more than the currently available balance (the total amount you added using the Deposit method), the Withdraw method raises an exception, and you will see the message "Insufficient funds available" in the Exception text box in the main QuickStart application window. This is because the Withdraw method contains a check that the withdrawal amount does not exceed the current balance; it raises an ArithmeticException if it does exceed the current balance.

However, the Exception Handling Application Block configuration specifies that an ArithmeticException will be wrapped in a new ApplicationException with the Message property set to the string "Insufficient funds available". The code in the MainForm class that handles the button click event and calls the method in the BankAccount class traps this exception and displays this message in the Exception text box.

Note

Because the Validation Handler is added to the pipeline using a directly applied attribute, it executes before the handlers declared in the application configuration. Therefore, if validation fails, the pipeline short-circuits without executing the other handlers. Therefore, no entry appears in the audit log file or the application event log, the Authorization Handler does not raise an exception, and the Performance Counters Handler does not update its counters.