72 msdn magazine
Unity (not to be confused with the Unity3D game engine) is a
general-purpose, extensible, dependency injection container with
interception support for use in any type of Microsoft .NET Framework-
based application. Unity is designed and maintained by the
Microsoft patterns & practices team (microsoft.com/practices). It can
be easily added to your application via NuGet, and you’ll fi nd the
main hub of learning resources related to Unity at msdn.com/unity.
Th is article focuses on Unity interception. Interception is a tech-
nique that’s useful when you want to modify the behavior of individual
objects without aff ecting the behavior of other objects from the same
class, very much as you’d do when using the Decorator pattern (the
Wikipedia defi nition of the Decorator pattern can be found here:
bit.ly/1gZZUQu). Interception provides a fl exible approach for adding
new behaviors to an object at run time. Th ese behaviors typically
address some crosscutting concerns, such as logging or data vali-
dation. Interception is oft en used as the underlying mechanism for
aspect-oriented programming (AOP). Th e runtime interception
feature in Unity allows you to eff ectively intercept method calls to
objects and perform pre- and post-processing of these calls.
Interception in the Unity container has two main components:
interceptors and interception behaviors. Interceptors determine the
mechanism used to intercept the calls to methods in the intercepted
object, while the interception behaviors determine the actions that
are performed on the intercepted method calls. An intercepted
object is supplied with a pipeline of interception behaviors. When a
method call is intercepted, each behavior in the pipeline is allowed
to inspect and even modify the parameters of the method call, and
eventually the original method implementation is invoked. Upon
return, each behavior can inspect or replace the values returned
or exceptions thrown by the original implementation or the pre-
vious behavior in the pipeline. Finally, the original caller gets the
resulting return value, if any, or the resulting exception. Figure 1
depicts the interception mechanism.
Th ere are two types of interception techniques: instance inter-
ception and type interception. With instance interception, Unity
dynamically creates a proxy object that’s inserted between the
client and the target object. Th e proxy object is then responsible
for passing the calls made by the client to the target object through
the behaviors. You can use Unity instance interception to intercept
objects created both by the Unity container and outside of the con-
tainer, and you can use it to intercept both virtual and non-virtual
methods. However, you can’t cast the dynamically created proxy
type to the type of the target object. With type interception, Unity
dynamically creates a new type that derives from the type of
the target object and that includes the behaviors that handle the
ASYNC PROGRAMMING
Intercepting
Asynchronous Methods
Using Unity Interception
Fernando Simonazzi and Grigori Melnik
This article discusses:
- The runtime interception feature in Unity
- Intercepting TAP asynchronous methods
- Dealing with generics
- An alternative to using await
Technologies discussed:
C# 5.0, Unity 3
Code download available at:
msdn.microsoft.com/magazine/msdnmag0 214