Aspect Oriented Programming (AOP) is a technique for applying cross-cutting concerns to existing implementations, without modifying them. Some examples of it are:
- Wrapping method calls that go to the database in transactions automatically;
- Logging all calls to some method, including the input parameters and return value;
- Catching exceptions thrown in a method automatically and doing something with them.
AOP is supported in the Enterprise Library (of which Unity is part) by the Policy Injection application block, and it can be integrated with Unity. You must install this application block, perhaps by using NuGet:
We need to add the interception behavior – which is the one that actually applies aspects – to Unity, either by code:
Or by XML configuration:
Having said that, the first concept we need to know is that of an interceptor. An interceptor in the Policy Injection block is an implementation of Microsoft.Practices.Unity.InterceptionExtension.IInterceptor interface, and there are three implementations:
It is required that, when you are going to apply an aspect to a registration, you choose an interceptor suitable for that registration, based on what type we are registering.
An aspect itself is an implementation of Microsoft.Practices.Unity.InterceptionExtension.ICallHandler, Unity includes five out of the box such handlers:
Authorization handler (Microsoft.Practices.EnterpriseLibrary.Security.PolicyInjection.AuthorizationCallHandler): for implementing authorization rules;
Exception handling handler (Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler): for applying the rules of the Exception Handling application block;
Logging handler (Microsoft.Practices.EnterpriseLibrary.Logging.PolicyInjection.LogCallHandler): for logging calls before and after a method call using the Logging application block;
Validation handler (Microsoft.Practices.EnterpriseLibrary.Validation.ValidationCallHandler): for validating arguments of a method with the Validation application block;
Performance counter handler (Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.PerformanceCounterCallHandler): for updating a performance counter upon calling a method.
The ICallHandler interface only defines a single method, Invoke, which wraps a method’s arguments and allows having code run before, after or even instead of the target method, and an Order property, for specifying the order by which the aspect should be applied, in case there are many.
A simple call handler, for outputting some string before or after a method call, might be:
Another option is to have interception for all methods of the target registration, which can be achieved by implementing Microsoft.Practices.Unity.InterceptionExtension.IInterceptionBehavior in a concrete class, such as this:
If we want to cancel the default method call, if it is non void, we must return an appropriate value:
Or if we want to return an exception:
There are three ways by which we can apply an aspect to a registration:
By applying an attribute to a method on the declaring or target type;
By code configuration;
By XML configuration.
Interception By Attributes
We need to create an attribute that derives from Microsoft.Practices.Unity.InterceptionExtension.HandlerAttribute and which instantiates our call handler:
And we apply it to any method declaration:
But before this works, we need to tell Unity to use interface interception for our type:
Interception By Code
For intercepting by code, whenever we register something with Unity, we also tell it to use interface interception and to include a behavior instance – it is not possible to specify a call handler for a specific method:
Interception By Configuration
When applying interception by configuration we also cannot target a specific method, but instead specify an interception behavior, which will apply to all method – of course, inside of it we can do our own filtering, by looking at the IMethodInvocation.MethodBase property:
You must Unity to retrieve an instance, which will be properly wrapped in a proxy, and from there all of your configured interceptors will be called:
Next in line: extending Unity.