AOP through Unity interceptors - Introduction

Hello,

Today we will start a serie of blog posts concerning Aspect Oriented Programming (formely AOP).

This technic is used to have a better separation of concerns in the application you write.
Let's take a basic example. When you are writing the implementation of a class that is specialized on a task, you could write someting like this:

public class Calculator
{
    public decimal Add(decimal num1, decimal num2)
    {
        Console.WriteLine("Entering Add Method");
        var result = num1 + num2;
        Console.WriteLine("Exiting Add Method");
        return result; 
    }
}
The issue with this code is that the logging is part of the method. What will happen if you have a huge quantity of code? You will probably duplicate the logging code across te different class & methods you will write. Moreover, the Add method we wrote is not specialized on a single task: it has two different purpose: it effectly performs an adds operation and it logs.

We will use Unity as an Inversion of Control framework to attach specific behavior to the code write.
As you may know, Unity uses a proxy to represent the object you are trying to resolve. When resolved, before & after a method call, you can introduce specific code through Behaviors. Unity proposes three kinds of interception: InterfaceInterceptor, TransparentProxyInterceptor & VirtualMethodInterceptor. If you want more explanation about those, you can visit https://msdn.microsoft.com/en-us/library/dn178466%28v=pandp.30%29.aspx. In our articles, we will use the InterfaceInterceptor. Here is the pipeline applied to the associated behaviors.

Note: this representation is inspired by https://msdn.microsoft.com/en-us/library/dn178466%28v=pandp.30%29.aspx

Let's have a look at the different steps:
  1. Client calls the Resolve method
  2. One method is called upon the interface
  3. The proxy calls the Invoke method on the first interception behavior
  4. The Behavior1 calls the Invoke method on the next Behavior, without explicitely knowing it
  5. While there is behavior in the pipeline, the operation is computed
  6. When there is no more  chained behavior, the method is processed upon the real implementation
  7. The real object returns to the last behavior
  8. While there is behavior in the pipeline, a post operation can be computed
  9. The first behavior is called
  10. The result is passed to the Proxy object
  11. The result is passed to the caller
To implement interception with Unity, you need Unity, of course, and Unity Interception Extension libraries. Then, to activate the interception, simply write this line when you declare your container:

IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();

In the next posts we will see when the aspect oriented programmation could be useful. We will write specific classes to handle different behaviors. This is an interesting way of instrumenting easily your application, make it go faster, reduce bandwidth consumption...

Comments

Popular posts from this blog

EMGU with Xamarin Forms guide [part 1]

Xamarin Forms essential: FFImageLoading, manage your images

[SocialCut] released! Make instagram grids, panorama and more