The Windows Forms 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 Windows Forms QuickStart represents a simple banking application that allows you to make deposits, withdrawals, and balance inquiries. It demonstrates how you can use the Policy Injection Application Block to implement authorization, logging, exception handling, and performance counters using handlers. Figure 1 shows the main form of the QuickStart.

Ff649822.41f8205b-b730-4d38-8dbf-2e07351e931f(en-us,PandP.10).png

Figure 1
The main form of the Policy Injection Windows Forms QuickStart

To understand how to run the QuickStart, and understand the configuration it uses, see the following sections and topics:

Building and Running the Windows Forms QuickStart

To review the way the QuickStart works, you must compile (build) it before running it. You use Visual Studio to build the QuickStart.

To build the Policy Injection QuickStart

  1. On the taskbar, click Start, point to All Programs, point to Microsoft patterns & practices, point to Enterprise Library 4.0 — May 2008, and then click Enterprise Library 4.0 Source Folder.
  2. In Windows Explorer, open the QuickStarts folder, open the PolicyInjection folder within it, and then open either the CS or VB subfolder, depending on the language you want for the QuickStart.
  3. Double-click the PolicyInjectionQuickStart.sln solution file to open the project into Visual Studio.
  4. On the Build menu, click Build Solution.
  5. Press F5 to run the Policy Injection QuickStart.

The Windows Forms QuickStart Configuration

The Policy Injection Windows Forms QuickStart ships with a defined configuration included in the App.config file. You can view or change these settings in three different ways:

  • In Visual Studio Solution Explorer, double-click App.config (or right-click it, and then click Open) to display the configuration file content as XML in the standard XML editor window. In this view, you can see the actual content of the configuration file and the elements and attributes for each section. However, you will usually find it easier to understand and manage configuration using one of the two following approaches.
  • In Visual Studio Solution Explorer, right-click App.config, and then click Edit Enterprise Library Configuration. This opens the Configuration Editor add-in that Enterprise Library registers with Visual Studio when you install it. The Visual Studio Properties window shows the values of each node in the configuration tree, and allows you to edit them.
  • On the taskbar, click Start, point to AllPrograms, point to Microsoft patterns & practices, point to Enterprise Library 4.0 — May 2008, and then click Enterprise LibraryConfiguration. This opens the stand-alone Configuration Console. Click the Open button on the toolbar, navigate to the QuickStart folder, and then select the App.config file. This view provides a comprehensive environment for editing configuration, but is not as easily accessible as the Configuration Editor when you are working on a project in Visual Studio.

Figure 2 shows the App.config file for the Policy Injection Windows Forms QuickStart open in the Visual Studio Configuration Editor. In this screen shot, the main window (the area on the left in the screen shot) contains the configuration tree showing all the configured objects from Enterprise Library. The Properties window (the central pane) shows the properties for the selected node in the configuration tree, and the Solution Explorer window on the right shows the normal view of the solution files.

Ff649822.c897cd3c-1062-4168-bb58-b6071966ae84(en-us,PandP.10).png

Figure 2
The QuickStart configuration open in the Visual Studio Configuration Editor

The QuickStart uses the following three matching rules provided with the Policy Injection Application Block:

  • Namespace Matching Rule. This is configured to select all members of classes that have the namespace PolicyInjectionQuickStart.BusinessLogic.
  • Member Name Matching Rule. This is configured to select only members named Withdraw.
  • Type Matching Rule. This is configured to select only classes of type BankAccount.

The QuickStart uses the following five handlers provided with the Policy Injection Application Block:

  • Logging Handler. This is configured to write log entries both before and after a method call with Priority 0 and Severity "Information."
  • Authorization Handler. This is configured with the operation name set to the "{method}" placeholder so that the authorization provider will apply the authorization rule identified by the current method name.
  • Performance Counter Handler. This is configured to update all counters except the "Total" counter.
  • Exception Handling Handler. This is configured to use the exception policy named "Bank Account Policy."
  • Validation Handler. This is added through attributes instead of through the configuration; therefore, it is not visible in Figure 2.

You can see in Figure 2, or by viewing the configuration yourself, that the QuickStart specifies the following two policies for the Policy Injection Application Block:

  • Authorize and Audit. This policy uses a Namespace Matching Rule to select classes for which the application block will generate a handler pipeline containing the Logging Handler and the Authorization Handler. The Namespace Matching Rule selects all members of classes that have the namespace PolicyInjectionQuickStart.BusinessLogic, within which the business object used by the QuickStart resides.
  • Exception Handling. This policy uses a Member Name Matching Rule and a Type Matching Rule to select classes for which the application block will generate a handler pipeline containing the Performance Counter Handler and the Exception Handler. The Member Name Matching Rule selects only members named Withdraw, and the Type Matching Rule selects only classes of type BankAccount. Therefore, as the rules combine with AND logic, the Exception Handling policy will only apply to the Withdraw method of the BankAccount class.

Other relevant sections in the configuration are the following:

  • Application Settings. This defines the three users Alice, Bob, and Charlie; and their employment types (Teller, Assistant, and Janitor).
  • Exception Handling Block. This specifies an exception handling policy named Bank Account Policy that generates an "Insufficient funds available" exception when an arithmetic exception occurs.
  • Logging Application Block. This specifies the formatting of the log file named Audit.log that the application generates within its run-time folder.
  • Security Application Block. This provides authorization for each user for the three tasks (Deposit, GetCurrentBalance, and Withdraw) based on each user's employment type.

Note

Each time you build and run the QuickStart, Visual Studio copies the App.config file to the output directory for the project (the same directory where it creates the QuickStart executable file) and renames it to PolicyInjectionQuickStart.exe.config. If you want to use the configuration tools to change any of the configuration settings, and you are planning to rebuild the solution, you must change the settings by opening the App.config file located in the QuickStart source directory. If you use the configuration tools or any other editor to change the configuration of the executable by editing the PolicyInjectionQuickStart.exe.config file, Visual Studio will overwrite these changes during the next successful build with the values in the App.config file.