Share via


Classes and Start-up Code in the QuickStart

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.

The Policy Injection QuickStart contains three classes:

  • MainForm.cs. This generates the main form that you see when you run the application. This class contains the event handlers for the buttons on the form and the code to populate the drop-down list of users and initialize the business object the application uses.
  • AmountEntryForm.cs. This generates a small dialog box where the user can enter an amount to deposit or withdraw. It contains a Visual Studio ErrorProvider control that ensures the amount entered is a valid numeric value.
  • BankAccount.cs. This provides the business logic for the application. This class exposes three methods named GetCurrentBalance, Deposit, and Withdraw that the application uses to manipulate the account.

Application Start-up Tasks

When the application starts, the constructor of the MainForm class calls the PopulateUserList method to create a list of users and populate the Current User drop-down list, as shown in the following code example.

private void PopulateUserList()
{
  const string userPrefix = "User:";
  foreach (string setting in ConfigurationManager.AppSettings)
  {
    if (setting.StartsWith(userPrefix))
    {
      string userName = setting.Substring(userPrefix.Length);
      string role = ConfigurationManager.AppSettings[setting];
      IPrincipal principal = new GenericPrincipal(
                 new GenericIdentity(userName), new string[] { role } );
      userComboBox.Items.Add(new KeyValuePair<string, IPrincipal>(userName, principal));
    }
  }
  userComboBox.SelectedIndex = 0;
}
'Usage
Private Sub PopulateUserList()
  Const userPrefix As String = "User:"
  For Each setting As String In ConfigurationManager.AppSettings
    If setting.StartsWith(userPrefix) Then
      Dim userName As String = setting.Substring(userPrefix.Length)
      Dim role As String = ConfigurationManager.AppSettings(setting)
      Dim principal As IPrincipal = New GenericPrincipal( _
          New GenericIdentity(userName), New String() {role})
      userComboBox.Items.Add(New KeyValuePair(Of String, IPrincipal)(userName, principal))
    End If
  Next
  userComboBox.SelectedIndex = 0
End Sub

This method takes the list of names from the application configuration (each identified by the "User:" prefix) and the matching role for each one. It generates a GenericPrincipal for each user that contains the user's name and role, and then adds this to the drop-down list in the main form. Generating an Identity and a GenericPrincipal allows the authorization features of the Security Application Block to identify users and check whether they are authorized according to the rules defined in the configuration of the Security Application Block.

Finally, the constructor of the MainForm class generates an instance of the BankAccount class to store and manipulate the details of the bank account. Notice that it uses the Create method of the PolicyInjection factory class to allow the Policy Injection Application Block to discover and apply any relevant policies.

bankAccount = PolicyInjection.Create<BusinessLogic.BankAccount>();
'Usage
bankAccount = PolicyInjection.Create(Of BusinessLogic.BankAccount)()