E.Interception pattern

  1. Summary

      1. Interception is a design pattern that is designed for cross-cutting concerns, issues that cut across the entire software.

      2. This is mainly used to put some special handling before or after method calls to handle the cross-cutting concerns, get the code out of the methods, and enhance its maintainability.

      3. Technically interceptors are proxy and generally is used in references of IOC Containers like Unity and core part of AOP.

  2. Types of interceptors

  3. The two styles of interception are instance interception and type interception.

        1. 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.

        2. 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.

      1. The client behavior is the same as when interacting directly to the real object, but the proxy intercepts the calls and manages their execution by collaborating with the real object and other objects as required.

      2. This pattern can further be used to Implement

  1. Implementation techniques

    1. Static Weaving (Post Sharp) : Uses IL instrumentation.

    2. Transparent Proxy (Unity) : Uses interface or inheritance based proxy

    3. Dynamic Proxy (Spring.NET, Castle) : Create proxy at run time by dynamically emitting Proxy IL code.

  1. Uses/Example

      1. Extending Default behavior of IOC containers like Unity Application Block

      2. Implementing method tracing and logging.

  1. Specific Considerations

  1. Related Pattern

      1. Proxy : are most common approach to implement interceptors.