
Instance and Type Inteceptors
The two styles of interception are instance interception and type interception.
Instance Interceptors
Instance interceptors use a separate proxy object between your code and your target object. Using interception, you make a call on the proxy object instead of directly calling the target object. The proxy invokes the various call handlers, and then it forwards the call to the target object. Different implementations of instance interceptors can have different constraints. Instance interceptors have the following characteristics:
- They can intercept objects created by the container.
- They can intercept objects not created by the container.
- They can only be used with interfaces or marshal-by-reference objects unless they are a TransparentProxyInterceptor, which works on interfaces and marshal-by-reference objects.
- Instance interceptors can be used to intercept on only one interface of an object unless they are a TransparentProxyInterceptor, which can be used to intercept several interfaces.
Type Interceptors
Type interceptors create a new type that inherits from the target type. This new type is then instantiated instead of the original type you requested. It overrides all the virtual methods on your target type. There is only one object instead of two as with instance interceptors. Type instance interceptors have the following behaviors:
- There is only one object created.
- They have full type compatibility because they are derived from the target type.
- They are able to intercept ojects only at creation and cannot intercept existing instances.
- They can only intercept virtual methods.
The Unity Interceptors
The Unity Interception extension ships with two instance interceptors and one type interceptor. The following table lists Unity's interceptors and their description.
|
Type
|
Description
|
Use
|
|
TransparentProxyInterceptor
|
An instance interceptor. The proxy is created by using the .NET TransparentProxy/RealProxy infrastructure.
|
When the type to intercept is a MarshalByRefObject or when only methods from the type's implemented interfaces need to be intercepted.
|
|
InterfaceInterceptor
|
An instance interceptor. It can proxy only one interface on the object. It uses dynamic code generation to create the proxy class.
|
When resolving an interface mapped to a type.
|
|
VirtualMethodInterceptor
|
A type interceptor. It uses dynamic code generation to create a derived class that gets instantiated instead of the original, intercepted class and to hook up the call handlers.
|
When only virtual methods need to be intercepted.
|
Selection of a specific interceptor depends on your specific needs, because each one has various tradeoffs. The following table summarizes the three interceptors and their advantages and disadvantages.
|
Type
|
Advantages
|
Disadvantages
|
|
TransparentProxyInterceptor
|
Can intercept all methods of the target object (virtual, non-virtual, or interface).
|
The object must either implement an interface or inherit from System.MarshalByRefObject. If the marshal by reference object is not a base class, you can only proxy interface methods. The TransparentProxy process is much slower than a regular method call.
|
|
InterfaceInterceptor
|
Proxy supports casting to all the interfaces or types of the target object.
|
It only intercepts methods on a single interface. It cannot cast a proxy back to target object's class or to other interfaces on the target object.
|
|
VirtualMethodInterceptor
|
Calls are much faster than the TransparentProxyInterceptor.
|
Interception only happens on virtual methods. You must set up interception at object creation time and cannot intercept an existing object.
|
You can set up a container for interception by using the Unity interception configuration elements. UnityConfigurationSection is the type name and by convention the element name is <unity>.
For information about using Enterprise Library and Policy Interception Application Block handlers, search the Enterprise Library Help file for "Call Handlers" or see Configuring and Using Pipeline Handlers on MSDN.
For more information about matching rules, see Configuring and Using Matching Rules.
The following code example uses the <UnityConfigurationSection> from a configuration file to configure a container for interception.
public static void UnityWithConfigInterception()
{
IUnityContainer container = new UnityContainer();
UnityConfigurationSection config = ConfigurationManager.GetSection("unity") as UnityConfigurationSection;
config.Containers.Default.Configure(container);
DoYourCode(container.Resolve<AccountManager>());
}
Public Shared Sub UnityWithConfigInterception()
Dim container As IUnityContainer = New UnityContainer()
Dim config As UnityConfigurationSection = ConfigurationManager.GetSection("unity") as UnityConfigurationSection
config.Containers.Default.Configure(container)
DoYourCode(container.Resolve(Of AccountManager)())
End Sub
For more information about interceptor configuration, see Configuration Support for Interception.