Thursday, June 10, 2010

Aspect Oriented Programming

Waiting at Dubai airport, for the connecting flight back home to Karachi... reaping the fruits of last-minute-flight-schedule sown(read: reserved) by my admin department! I have a tweltve hour wait for the next "connecting" flight! Yep, those who have been-there-done-that know very well about such an exhausting experience.

With all the "unlimited" time that I have, I opened my 'Blogs' folder... searched through my incomplete blog related to aspect oriented programming.

I have been working over it off an on, adding multiple angles of understanding so that it would be easiest to digest.

Though this has been a pretty old subject now; but some once again was dragged into the technical argument among peers. Quite a couple of hours ranting about what is it, what it does, and when to use. On heading home I thought its a "must blog" (0: so here it is.

To understand the aspect oriented programming, it is important to understand the term aspect. Aspect generally means an area of particular interest/concern; a much widely used term that is the jist of AOP is cross cutting concerns.

So what actually is a "cross cutting concern"?

To understand that, lets discuss a small example. Consider following set of classes:

1. Products - This class add/update the products
2. Customers - This class maintains customers
3. AuditTrails - This class maintains the audit trail
4. UI - This class renders the UI
5. Logger - Logs the activity in a text file

A business "concern" could come up to implement a policy that would validate the length product name, and it should not be more than 20 characters. Or...
  • A system level concern may be to provide logging in all of the modules.
  • Another concern could be to ensure a database transaction should not result in loss of data - across all the data layer related modules.
  • Or that authorization is required to perform certain operations; for instance authenticate/authorize for Funds Transfer in case of a banking application.
The above are called "concerns" - specific area of interest; and since these concerns "can" span across the application(in case of logging, for instance), so this is how it gets the name "cross cutting concern".


A couple of practical scenarios where AOP could be utilized:

  • Logging invocation and public property access, as discussed earlier.
  • Handling exceptions in a structured manner
  • Validating parameter values; function parameters.
  • Caching method results and property values
  • Authorizing method and property requests
  • Measuring target method performance

Typically, if you want to enforce a behaviour across many different classes in a transparent way, AOP comes a best practice.

If you are a Java guy AspectJ might interest you; and for .NET/C# a good open library is Aspect.NET. Though Microsoft does provide Policy Injection Application block which can be utilized with Unity (Dependency Injection Framework - DI) application block that I reviewed sometime earlier in my posts.



Terms that are used in AOP:

From Wikipedia:
Cross-cutting concerns: Even though most classes in an OO model will perform a single, specific function, they often share common, secondary requirements with other classes. For example, we may want to add logging to classes within the data-access layer and also to classes in the UI layer whenever a thread enters or exits a method. Even though each class has a very different primary functionality, the code needed to perform the secondary functionality is often identical.
Advice: This is the additional code that you want to apply to your existing model. In our example, this is the logging code that we want to apply whenever the thread enters or exits a method.
Pointcut: This is the term given to the point of execution in the application at which cross-cutting concern needs to be applied. In our example, a pointcut is reached when the thread enters a method, and another pointcut is reached when the thread exits the method.
Aspect: The combination of the pointcut and the advice is termed an aspect. In the example above, we add a logging aspect to our application by defining a pointcut and giving the correct advice.
Bear in mind, you will pay a performance penalty if you're injecting behaviour into many fine-grained objects in your application, so you may want to consider if having this feature is worth the performance trade-off. Fortunately, one of the benefits of AOP is that you can enable/disable pointcuts fairly easily, which would allow you to measure the performance difference. If interested in knowing the code level implementations, here is a good article to start with.

Happy coding!

1 comment:

  1. Anonymous11:38 PM

    Nice one Kamran.I ghave just seen a very nice presentation at http://www.blip.tv/file/3655576?filename=InetaLive-20100515_2_Keyvan_Nayyeri100.flv

    ReplyDelete

Related Posts

Popular Posts