Wednesday, September 28, 2011

Aspect oriented programming (AOP) using Castle Windsor

In this post, I am going to discuss AOP methodology using Castle Windsor. There are always multiple ways to code the given functionality. It is advisable that the coder should always analyze pros and cons of each technique before implementing the code. The technique with less number of disadvantages yields good code which is easy to maintain and extend over a period of time. The good quality software should satisfy both the functional implicit / non-functional requirements and should be understood better by any developer.

Typically a software is made up of blocks which are often known as components analogous to the book made up of pages. Generally components are seen as a functionality or the subset of it.Components can also work as the helpers to the other components that abstracts away the common tasks from the main components and makes the code more reusable. The software can have many different horizontal services (common components) which are often implemented separately from the main components. The components that can be treated as a horizontal services could be logging, exception handling, authentication, basic pre-conditions, etc. These components can be implemented separately in all the functions but the code may become cumbersome to maintain. On other hand, we can define common code which handles these functionalities outside the method body and hence keeps the code cleaner and more readable.This technique is known as aspect oriented programming (AOP). Let’s evaluate both the technique individually.

Horizontal services as a part of main components: This is the conventional and traditional way of implementing supporting functionalities. The common functionalities is mingled with the main components in this approach. Consider the following example for authentication and logging functionality:

Consider that the code mentioned above is common for all the function across the whole product.In such cases, the number of lines of code increases if it is repeated in all the functions.Additionally, the changes in the logic needs to be reflected in all the functions. Supposing the average product has around 300 different types of functions then it becomes very difficult for a developer to manually change all these functions to reflect common functionality everywhere. It also becomes difficullt for a programmer to establish common code for these functionalities because all these functions may have different signatures and number of parameters.

Obviously to get rid of such nightmares we need a framework which provides efficient, convenient and uniform way of reusing the code. This is where AOP pitches in. The AOP is implemented using interceptors to the functions. The interceptors are functions that gets executed when the main function is invoked. The interceptors defined in the type are registeredagainst the types used in the product e.g. domain entity or repositories. Lets discuss aspect oriented programing using Castle Windsor.

Aspect oriented programming with Castle Windsor: The Castle Windsor is open source project that provides framework to implement AOP methodology. Lets have a look at the code below:

Code snippet - 1

Code snippet - 2

The snippet-1 defines the logging aspect which can be applied to all the functions across the product. The class implements Iinterceptor interface which is located in Castle.DynamicProxy namespace. The interface has Intercept() which gets executed everytime the function is invoked from its registered type. The interceptors are registered for a particular type as shown in snippet-2. The tangible advantage of using this methodology is to get rid of writing the code for logging across all the functions. Instead, each function call gets intercepted by a separate class and the code gets executed for all the functions. The call is redirected to original function using Proceed(). The arguments to the function is encapsulated in the reference of Iinvocation interface which is passed as a parameter to Intercept(). The Castle Windsor also provides the standard implementation of IInterceptor interface in the framework class called StandardInterceptor. This class provides following three methods:

  • PreProceed : Gets executed before any function gets executed. This can be used to assert the pre-conditions for the function.
  • PerformProceed : Includes the call to the actual function.
  • PostProceed() : Gets executed after any function gets executed. This can be used to log the function output for diagnosis purpose.


There can be multiple interceptors registered for a type. The code for registering multiple interceptors is as follows:

Aspect oriented programming is a methodology that helps the programmer to write a clean and modular code. There are many terminologies associated to AOP which is out of the scope of this post. You can find more details related to aspect oriented programming with Castle Windsor at http://docs.castleproject.org/(S(mbos0245qatumh55n2vcpjag))/Windsor.Introduction-to-AOP-With-Castle.ashx

The greater separation of concerns, the cleaner source code!

No comments: