Aspect Oriented Programming – your good friend

Team_without_starRecently I have been working on quite a big website. From the very beginning I knew I would need data caching layer or something that will smartly do it. The simplest solution is just to check if something is in cache in my method code and take it from there. Well, but what if I have 100 methods? Should I call the same logic in all of them? This is only one method call, but still 100 times, makes me feel “this is not the smartest way to do it”. Fortunately I have found out a new approach to make my caching very robust and smart. It is called Aspect Oriented Programming. By using it I have reduced the amount of my code significantly.

Object Oriented Programming is like defining players in the field

Programming is an art of dividing your functionalities into objects – I call them players. They all are playing in one team to meet the goal of the program. The main issue is when all your players have to pass the ball to one specific team member to play well (lets call this player – a star). What happens when star is sick? (or in code jargon – it is reimplemented, and it’s is causing some bugs). Your team is not able to play well anymore because the whole glory and pressure is on the star’s shoulders. Even if you have divided the responsibilities well and each of the players knows their role, your team setup is very weak. Team_with_star

Make your star to step back

Aspect Oriented Framework allows you to solve such problems in your team (code). The way it works is very magic. Aspect Oriented Framework allows your star player to be in between of each of your players but they do not see him. They are playing like he is not there, but he helps and controls the ball. So your star is no longer a player in the field but he watches each ball passage – like a coach. Team_without_star

Back to code – where to put logic that is shared between a few objects?

Let’s assume you want to implement logging in your application. It means that the logic responsible for putting new information into log will be executed in several places in your application. This can lead to high coupling of your objects with the logging module because in almost each method you will launch a logging method.

With AOP Framework you can capture method call before it is executed

Purely, AOP Framework allows you to capture your code before it executes the next method and decides what to do now. Your star player (your aspect) decides whether the ball is correctly passed to the other team member. Star player can even change the direction of the ball to make it reach the correct aim.

Your first level code is now not poluted with second level features

Using AOP Frameworks you can focus on your business logic and aspects like caching, authorization, logging, tracing and others are not spread through whole application. They are implemented in one place capturing almost method call and let it go further after doing their job.

Be a good coder and find your star player before he makes your team stop playing well

Looking at your code like on a team with players allows you to personalize your modules. Make them more similar to humans. If you see that one of your players is too often used in all around, maybe he should be taken out from the team and become a coach. In my next post I will write about PostSharp AOP Framework, which made me very happy recently because I have implemented caching aspect in a very neat and clean way.

IT guy with head full of ideas, strongly oriented on achieving great goals in life. Currently working for Aspire Systems Poland, place where I grow.