Friday, August 17, 2007

Policy Injection Application Block

Introduction

The purpose of the Policy Injection Application Block is to separate the core concerns and crosscutting concerns.

A policy is a collection of matching rules that specifies how the Plolicy Injection Application Block selects classes and members to which it will attach a handler pipeline and a collection of the handlers that make up that pipeline.

A policy is not limited to a single class or a class member; depending on the matching rules, it can apply to all the members of one class, all the members of several classes, some members of one class, some members of multiple classes or some other combination.


Using the Create Method

1. It creates an instance of the target class.
2. It creates suitable proxy for the class or for the specified interface that the class implements.
3. It wires up the specified handlers into a pipeline between the relevant methods and properties of the proxy and the concreate target class instance.
4. It returns a reference to the proxy.

The Policy Injection Application Block addresses the following scenarios

-building applications from objects
-managing cross-cutting concerns
-allowing the developer and administrator to configure the behavior of objects

Design goals
- allow policies to be dynamically applied to any business objects
- allow policies to be modified without changing the code
- minimize the performance impact of checking for and applying policies at run time
- provides extensibility of handlers, matching rules, and interception mechanism
- allow developers to create and reuse key parts of the infrastructure independently

Scenarios for Using the Policy Injection Application Block
1. Logging Method Invocation and Property Access
allow developers and administrators to oconfigure logging to the Windows Event Log, e-mail messages, databases, message queue systems, text files, WMI events, or custom locations.
2. Handling Exceptions in a Structured Manner.
They include
Logging exception information
Hiding sensitive information by replacing the original exception with another exception
3. Validating Parameter Value
this minimizes the code developers have to create and it allows administrators to modify the validation policies through configuration, if required
4. Caching Method Results and Property Values
if the application repeatedly calls methods during its execuotion, the caching can reduce the demands the application places on hardware and system resources and increase perceived response times.
The Caching Handler is also useful for caching the values of properties exposed by classes
5. Authorizing Method and Property Requests
The Policy Injection Application Block provides a handler that will check the authorization status of the requesting user or thread against the target class permissions before calling the target method or accessing the target property.
6. Measuring Target method Performance
The Policy Injection Application Block contains instrumentation that allows developers, administrators, and operators to monitor the performance of the application block itself.


How to incorporate the Policy Injection Application Block
1. Add reference
Microsoft.Practices.EnterpriseLibrary.PolicyInjection.dll
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.ObjectBuilder.dll

Using Microsoft.Practices.EnterpriseLibrary.PolicyInjection

2. Reordering Policies
If more than one policy matches a specified method, the application block will apply the policies in the order they occur in the configuration. It is possible to change the order of policies defined for the Policy Inojection Application Block.
Right-click the policy node you want to move, and then click either Move Up or Move Down.

3. Defining and Configuring Handler Pipelines.
The Handlers section of a policy defines the series of handlers that the Policy Injection Application Block will execute when client node calls a method or sets a property of the target class that corresponds to the matching rules defined for that policy
The Application Block executes the pre-processing task of each handler in the order that they occur in configuration.
The Application Block then calls the specified method
The Application Block then executes the post-processing task of each handler.

Key Scenarios and Development Tasks

1. Deciding if an Object is Interceptable
2. Objects that Derive From MarshalByRefObject
3. Objects that Implement a Known Interface
4. Creating and Wrapping Object Instances
5. Creating New Policy-Enabled Target Objects


built-in matching rules
1. The Assemply Maching Rule
allows developers, operators, and administrators to select target classes bases on the file name of the asssembly.
2. The Custom Attribute Matching Rule
allows to select target classes based on a custom attribute type that is appiled to class members.
3. The Member Name Matching Rule
allows to select target classes based on the name of the class members (methods or properties), including using wildcard characters for the member name.
4. The Method Signature Matching Rule
allows to select target classes based on the name and signature of its members.
5. The Namespace Matching Rule
allows to select target classes based on their namespace, using wildcard characters for the child namespace names but not for the root namespace name.
6. The Parameter Type Mathing Rule
allows to select target classes based on the type name of a parameter for a member of the target object.
7. The property Matching Rule
allows to select individual properties of the target classes based on their name. including using wildcard characters, and the combination of accessors they include.
8. The Return Type matching Rule
allows to select classes based on the type name of the return value, using wildcard characters for the type name.
9. The Tag Attribute Matching Rule
allows to select target classes based on the name of an attribute of type Tag that is applied to a class, or to members within a class, including using wildcard characters for the attribute name.
10. The Type Mactching Rule
allows to select target classes using the namespace and class names.
11. Custom Matching Rules
Developers can create custom matching rules that integrate with the Policy Injection Application Block.

Built-in handlers
1. The Authosization Handler
provides the capability to check that the current user has the requisite permission to access the selected object method or property. This handler uses the Security Apllication Block and takes advantage of the features that it exposes.
2. The Caching Handler
provides the capability to cache the value or object instance returned from the selected method or the value of the selected property. This handler using the Caching Appication Block. The Caching Hadler applies both beforeand after invocation of the selected method or setting of the selected property of the target object.
3. The Exception Handling Handler
provides the capability to manage and process exceptions in a standard way. This handler uses the Exception Handling Application Block. The Exception Handling Handler applies after invocation of the selected method or access to the selected propery of the target object.
4. The Logging Handler
The Logging Handler provides the capability to write log mesages and trae messages as the client code invokes the selected method or accesses the selected property of the target object. This handler uses the Logging Application Block. The logging Handler applies both before and after invoking the selected method.
5. The Performance Counter Handler
increments a specific counter each time it executes in response to invocation of the selected method or setting of the selected property. This handler uses the instrumentation features that are part of the Enterprise Library Core. THe Performance Counter Handler applies both before and after invocation of the selected method or access to the selected property of the target object.
6. The Validation Handler
The Validation Handler provides the capability to test whether the value provided for the selected property, or the values specified for the parameters of the selected method, are valid against rules. This handler uses the Validation Application Block. The Validation Handler applies the validation before invoking the method or setting the property of the target object.
7. The custom Pipeline Handler
Developers can create custom handlers that integrate with the Policy Injection Application Block.


Effective Policy Viewer
Is a tool that can analyze an assembly using a specified configuration file and discover the actual handler policy that will apply to the classes within the assembly.

No comments: