分享

Using Interception in Applications

 Wiley Library 2013-03-04

Using Interception in Applications

0(共 1)对本文的评价是有帮助 评价此主题

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.

Ff660848.6d45f678-9a61-4aef-8d4d-e5521b892112(en-us,PandP.20).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.

当你使用一个依赖注入容器,容器提供一个到被拦截的对象的引用。当容器按照拦截配置提供一个对象时,该对象会按照配置内容被拦截。当进行独立拦截时,你必须已经获取了需要进行拦截的对象的引用。你可以嗲用独立拦截API并显式得拦截该对象。

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.

Unity 拦截需要一个拦截器,以及一个拦截行为的管道集合。拦截器被使用当通过容器或独立API执行拦截。 使用容器,会引发相关的拦截对象被提供,而使用独立API是能照哦到进执行拦截。

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:

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多