Using a Different Interception Mechanism
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 interception mechanism used by the Policy Injection Application Block depends on standard .NET Framework remoting techniques. However, you can change the interception mechanism to one that better suits your custom requirements. For example, you may want to use a different communication channel for messages sent between handlers and the target object, or you may need to use a custom message type.
To create your own interception mechanism, first you will need to decide how you are going to intercept the calls to the target object and what will qualify an object as interceptable. Typically, this will involve a proxy object that executes the handlers and then calls the underlying target object. Details of proxy implementation vary depending on the interception mechanism chosen.
Connecting your new interception mechanism to the rest of the Policy Injection Application Block requires that you create a class that inherits from the PolicyInjector abstract base class. This class has two required and one optional method that you can implement:
- public abstract bool TypeSupportsInterception(Type t**)**. This method returns true if this interception mechanism can intercept calls to the type t. If the type is not interceptable, calls to PolicyInjection.Wrap or PolicyInjection.Create will throw an exception if there are policies defined for the target object.
- protected abstract object DoWrap(object instance**, Type** typeToReturn**, PolicySet** policiesForThisType**)**. This method receives an existing target object and should construct and return the appropriate proxy object for your injector.
- protected virtual object DoCreate(Type typeToCreate**, Type** typeToReturn**, PolicySet** policiesForThisType**, object[ ]** arguments**)**. This method is called to create new intercepted objects. The base class implementation uses Activator.CreateInstance to create the object, and then calls the DoWrap method. As a result, if you do not need to deal with the creation of intercepted objects, you do not need to override this method.
For configuration support, you can implement a configuration element by deriving from the InjectorData class. After you do that, you can use the Enterprise Library configuration tools to specify that the Policy Injection Application Block should use your injector as the default instead of the normal remoting proxy-based interception.
You can reuse the PolicySet, Policy, MatchingRule, and CallHandler classes within any interception mechanism implementation you choose.