?What Is Interception
- شنبه, ۳ مرداد ۱۳۹۴، ۰۲:۰۶ ب.ظ
What Is Interception?
Interception is an advanced programming technique that allows you to intercept calls to an object so that you can add additional logic before or after the call. This interception process should be virtually transparent, so both the calling object and the target object can be oblivious to the interception process.
The following diagram shows how instance interception typically works:
The calling object will typically ask the Unity dependency container for a target object. However, it will not receive a real TargetObject, but rather an intercepting proxy that is impersonating the real TargetObject, for example, by implementing the same interface. This way the calling object does not know if it calls the real target object or the proxy. The TargetObject also doesn't know that the calls have been intercepted. The fact that both classes don't know this about the interception process allows you to add custom logic before or after a member call, without having to change either the calling object or the target object.
Interception is quite similar to using the decorator pattern. With a decorator you'll typically have to program a custom decorator class for each target object. However, Unity will dynamically generate the interceptor for you, so all you have to do is implement the custom behaviors.
- ۹۴/۰۵/۰۳