Export (0) Print
Expand All

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:

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.


Ff660891.note(en-us,PandP.20).gifNote:
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.


Ff660891.note(en-us,PandP.20).gifNote:
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.


Ff660891.note(en-us,PandP.20).gifNote:
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.



Show:
© 2014 Microsoft