इसके माध्यम से साझा किया गया


Using Interception in Applications

patterns & practices Developer Center

Download codeDownload PDFOrder Paperback

This topic describes how to use Unity interception in your applications. You can use Unity interception with a dependency injection (DI) container or as a stand-alone feature with no DI container.

Dn507481.3397ADBCC9BA37E1603B111630672796(en-us,PandP.30).png

When you use a DI container, the container provides a reference to the intercepted object. When the container resolves an object that is configured for interception, the object is intercepted as defined in the configuration. When performing stand-alone interception, you must already have a reference to the object you want intercepted. You invoke the stand-alone interception API and explicitly intercept the object. For more information on using containers, see Dependency Injection with Unity.

Unity interception requires an interceptor, and a collection of interception behaviors that comprise a pipeline. Interceptors are used when performing interception through the container and through the stand-alone API. Using the container results in adding interception objects that are being resolved, while using the stand-alone API enables you to perform just interception.

In both cases you can implement additional interfaces on intercepted objects. You must provide the information for the additional interfaces that will be added for interception. Using additional interfaces has some limitations. You cannot add open generic interfaces and you cannot reimplement an interface that has already been implemented with nonvirtual methods. Additional interfaces are supported by all three types of interceptors. When you are performing interface interception, you can only invoke methods on the particular interface you used to intercept and any additional interfaces that you have specified. Casts to other interfaces implemented by the intercepted object will fail.

Though behaviors normally can have both pre- and post-processing functionality, with any additional interfaces the behaviors must handle the processing in its entirety. There cannot be any post-processing because the original object has no implementation for the additional interfaces’ methods. Any calls to the additional interface methods handled by the behaviors will result in a NotImplementedException being thrown.

You can also intercept abstract classes with abstract methods, allowing the behaviors pipeline to provide the implementation for these abstract methods. This is similar to adding interfaces in that in both cases there are undefined methods for which an implementation must be provided, but they are different mechanisms. Abstract class interception only works with the VirtualMethodInterceptor. You can implement abstract classes with abstract methods. If you intercept a type that already implements some interfaces, both virtual method and transparent proxy interception allow for casting to these interfaces and invoke methods on them, which will be intercepted. There cannot be any post processing because the original object has no implementation for the additional interfaces’ methods

This topic contains the following sections:

Next Topic | Previous Topic | Home | Community