Freigeben über


Interception with Unity

Unity interception enables you to effectively capture calls to objects and add additional functionality to the target object. Interception is useful when you want to modify the behavior for individual objects but not the entire class, very much as you would do when using the Decorator pattern. It provides a flexible approach for adding new behaviors to an object at run time.

This section contains the following topics that will help you to understand interception:

  • About Unity Interception. This section of this topic describes the basic principles of interception in Unity.
  • Scenarios for Interception. This topic describes common scenarios addressed by Unity interception.
  • Behaviors for Interception. This topic describes behaviors you might implement with the IInterceptionBehavior interface to configure a container through the RegisterType method.
  • Configuring a Container for Interception. This topic describes how to configure the Unity container for interception.
  • Unity Interception Techniques. This topic describes in detail the design of Unity interception.
  • Using Interception in Applications. This topic describes how you use Unity interception in your applications.
  • Using Interception and Policy Injection. This topic explains how policy injection through interception works in Unity, how you can use matching rules to select target classes and class members for policy injection, and how you can use the Enterprise Library call handlers with Unity.

About Unity Interception

Unity provides both instance and type interception, and enables interception with either objects you resolve through the container, or by using the stand-alone API to explicitly invoke interception on a known instance.

Note

Unity interception can be used without a Unity dependency injection (DI) container by using the stand-alone API through the static Intercept class. For more information see Using Interception in Applications.

The interception mechanism captures the call to an object at invocation time and provides the full implementation of the object. Unity uses the Interceptor class to specify the interception mechanism to be used, how interception happens, and the InterceptionBehavior class to describe what to do when an object is intercepted. Unity interception is designed to have its behaviors performed on the entire object and all of its methods. Interceptors play a role only at proxy (or derived type) creation time. Once the proxy, or derived type, is created, the interceptor has provided any required components for the intercepted object and incorporated them into the proxy's processing. The proxy can then complete its processing with no further input from the interceptor.

Note

Proxy or type-creation time is the point of the largest performance hit.

Unity interception uses a single behaviors pipeline encompassing all of the behaviors for processing interception. The pipeline consists of an essentially fixed series of elements, the behaviors that you have set up, that perform pre- and/or post-processing logic on the object. If so dictated by the behavior element logic, each behavior element in the pipeline can end the invocations, and subsequent pipeline elements will not execute. The last executed element in the pipeline exits the pipeline and invokes the implementation of the intercepted object, thus enabling run-time processing. In post processing the process is reversed.

Ff660891.9039f9b2-8c6b-453c-bdc1-414f52631444(en-us,PandP.20).png

The following table describes the basic components of the Unity interception mechanism.

Interception Component

Description

Interceptor

Represents an interception mechanism. The interception mechanism actually intercepts the object call. There are two kinds of interceptors, type and instance. Unity provides a single type interceptor, VirtualMethodInterceptor, and two instance interceptors, Interface and TransparentProxy. For more information on type and instance interceptors see Unity Interception Techniques.

Interception behavior

Represents what to do when a method is intercepted. Behaviors always apply to every method on an intercepted type. Policy injection is just one such behavior.

Behaviors pipeline

A pipeline of behaviors that processing is routed to for interception.

Note

Interception behaviors are a generalization of the default policy injection call handlers. The ICallHandler interface is very similar to the IInterceptionBehavior interface. A major difference is that the call handlers are passed to the method that they are called on.
The default policy injection call handlers target specific methods; but depending on the matching rules, they might target each and every method on an intercepted type, whereas Unity interception is designed to have its behaviors performed on the entire object and all of its methods.