Share via


Making a Deposit

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.

To make a deposit, select a user in the CurrentUser drop-down list, and then click Deposit. In the Deposit dialog box, enter the required deposit amount, and then click OK, as shown in Figure 1.

Ff648442.de345352-28b6-4680-a24f-261d03866534(en-us,PandP.10).png

Figure 1
Making a deposit in the Policy Injection QuickStart

The following extract shows the code that runs when you click the Deposit button. It creates a new instance of the AmountEntryForm, specifying the value Deposit 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 Deposit method of the BankAccount class. If an error occurs, the code places the message into the Exception text box.

private void depositButton_Click(object sender, EventArgs e)
{
  AmountEntryForm form = new AmountEntryForm(AmountDialogType.Deposit);
  DialogResult result = form.ShowDialog(this);
  if (result == DialogResult.OK)
  {
    exceptionTextBox.Text = String.Empty;
    try
    {
      bankAccount.Deposit(form.Amount);
    }
    catch (Exception ex)
    {
      exceptionTextBox.Text = ex.Message;
    }
  }
}
'Usage
Private Sub depositButton_Click(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles depositButton.Click
  Dim form As New AmountEntryForm(AmountDialogType.Deposit)
  Dim result As DialogResult = form.ShowDialog(Me)
  If result = DialogResult.OK Then
    exceptionTextBox.Text = String.Empty
    Try
      bankAccount.Deposit(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 Deposit Method

The configuration of the QuickStart causes the Policy Injection Application Block to create a handler pipeline containing the Logging Handler and the Authorization Handler for the Deposit method. In addition, the Deposit method carries a ValidationCallHandler attribute that, in conjunction with attributes on the depositAmount parameter, adds a Validation Handler to the pipeline that specifies the value passed to the method must be greater than zero.

[ValidationCallHandler]
public void Deposit([RangeValidator(typeof(Decimal), "0.0", 
                    RangeBoundaryType.Exclusive, "0.0", RangeBoundaryType.Ignore,
                    MessageTemplate = "Deposited amount must be more than zero." )] 
                    decimal depositAmount)
{
  balance += depositAmount;
}
'Usage
<ValidationCallHandler()> _
Public Sub Deposit(<RangeValidator(GetType(Decimal), "0.0", _
                   RangeBoundaryType.Exclusive, "0.0", RangeBoundaryType.Ignore, _
                   MessageTemplate:="Deposited amount must be more than zero.")> _
                   ByVal depositAmount As Decimal)
  balance += depositAmount
End Sub

Note

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

If the value entered is greater than zero, and the currently selected user is Alice, the Deposit method call will succeed. The Policy Injection Application Block executes the Validation Handler, the Logging Handler, and the Authorization Handler within the pipeline to the Deposit 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 Deposit method prevents invocation of the method and the Exception text box on the main form displays the message, "Parameter validation failed, Parameter name: depositAmount."

Effects of the Logging Handler

The Logging Handler appends two entries to the application's audit log file; one entry is appended before the Deposit method executes and one entry is appended after it completes. You can click the View Log File button to see the entire contents of the application's audit log file.

----------------------------------------
Timestamp: 03/03/2007 13:15:30
Message: Before
Category: General, Audit
Type: PolicyInjectionQuickStart.BusinessLogic.BankAccount
Method: Deposit
Parameters: depositAmount : 100
Return Value: 
Exception: 
Call Time: 
Priority: 0
EventId: 1
Severity: Information
Title:Call Logging
Machine: DIMINUTIVEC
Application Domain: PolicyInjectionQuickStart.vshost.exe
Process Id: 2492
Process Name: F:\EntLib3Src\QuickStarts\PolicyInjection\...\PolicyInjectionQuickStart.vshost.exe
Win32 Thread Id: 2732
Thread Name: 
----------------------------------------
Timestamp: 03/03/2007 13:15:30
Message: After
Category: General, Audit
Type: PolicyInjectionQuickStart.BusinessLogic.BankAccount
Method: Deposit
Parameters: depositAmount : 100
Return Value: 
Exception: 
Call Time: 00:00:00.0003195
Priority: 0
EventId: 1
Severity: Information
Title:Call Logging
Machine: DIMINUTIVEC
Application Domain: PolicyInjectionQuickStart.vshost.exe
Process Id: 2492
Process Name: F:\EntLib3Src\QuickStarts\PolicyInjection\...\PolicyInjectionQuickStart.vshost.exe
Win32 Thread Id: 2732
Thread Name: 
----------------------------------------

Effects of the Authorization Handler

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

Note

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