tags:

views:

877

answers:

9

I understand object oriented programming, and have been writing OO programs for a long time. People seem to talk about aspect-oriented programming, but I've never really learned what it is or how to use it. What is the basic paradigm?

This question is related, but doesn't quite ask it:

Aspect-Oriented Programming vs. Object Oriented Programming

+18  A: 

AOP addresses the problem of cross-cutting concerns, which would be any kind of code that is repeated in different methods and can't normally be completely refactored into its own module, like with logging or verification. So, with AOP you can leave that stuff out of the main code and define it vertically like so:

function mainProgram()
 { var x =  foo();
   doSomethingWith(x);
   return x;
 }

aspect logging
 { before (mainProgram is called):
    { log.Write("entering mainProgram");
    }

   after (mainProgram is called):
    { log.Write(  "exiting mainProgram with return value of "
                + mainProgram.returnValue);
    }
 } 

aspect verification
 { before (doSomethingWith is called):
    { if (doSomethingWith.arguments[0] == null) 
       { throw NullArgumentException();
       }

      if (!doSomethingWith.caller.isAuthenticated)
       { throw Securityexception();
       }
    }
 }

And then an aspect-weaver is used to compile the code into this:

function mainProgram()
 { log.Write("entering mainProgram");

   var x = foo();   

   if (x == null) throw NullArgumentException();
   if (!mainProgramIsAuthenticated()) throw Securityexception();
   doSomethingWith(x);   

   log.Write("exiting mainProgram with return value of "+ x);
   return x;
 }
Mark Cidade
Do you need language support for this, then? What language is your example in?
Sophie
This is pseudocode but the most well-known example is AspectJ, which is an AOP modification of Java, which uses a similar technique called cutpoints.
Mark Cidade
Actually, they`re called pointcuts :)
Don
Kind of like a Other or ETC class then? Or is because you're just using a very simplified example?
melaos
I wouldn't call logging and verification "miscellaneous" concerns. Although you could have a "Misc" or "Util" aspect but that would be kinda sloppy.
Mark Cidade
Voodoo. And I thought OOP was overkill.
Aiden Bell
A: 

AOP is a way to better modularize your application for functionality that spans across multiple boundaries. AOP is another way to encapsulate these features and follow Single Responsiblity by moving these cross-cutting concerns (logging, error handling, etc.) out of the main components of your application. When used appropriately AOP can lead to higher levels of maintainability and extensibility in your application over time.

SaaS Developer
+1  A: 

Take a look at this video by the creator of PostSharp ( a set of AOP libraries for .NET ): http://www.postsharp.org/about/video/

Really helped me grok it to see it in use.

jfar
+2  A: 

Unfortunately, it seems to be surprisingly difficult to make AOP really useful in a normal mid-large size organization. (Editor support, sense of control, the fact that you start with the not-so-important things leading to code-rot, people going home to their families, etc.)

I put my hopes to composite oriented programming, which is something more and more realistic. It connects to many popular ideas and gives you something really cool.

Look at an up and coming implementation here: qi4j.org/

PS. Actually, I think that one of the beauties with AOP is also its achilles heel: Its non-intrusive, letting people ignore it if they can, so it will be treated as a secondary concern in most organizations.

Hugo
A: 

Copied from a duplicate for completeness (Buzzer):

Class and method attributes in .NET are a form of aspect-oriented programming. You decorate your classes/methods with attributes. Behind the scenes this adds code to your class/method that performs the particular functions of the attribute. For example, marking a class serializable allows it to be serialized automatically for storage or transmission to another system. Other attributes might mark certain properties as non-serializable and these would be automatically omitted from the serialized object. Serialization is an aspect, implemented by other code in the system, and applied to your class by the application of a "configuration" attribute (decoration) .

paxdiablo
A: 

Copied from a duplicate for completeness (Einstein):

The classic examples are security and logging. Instead of writing code within your application to log occurance of x or check object z for security access control there is a language contraption "out of band" of normal code which can systematically inject security or logging into routines that don't nativly have them in such a way that even though your code doesn't supply it -- its taken care of.

A more concrete example is the operating system providing access controls to a file. A software program does not need to check for access restrictions because the underlying system does that work for it.

If you think you need AOP in my experience you actually really need to be investing more time and effort into appropriate meta-data management within your system with a focus on well thought structural / systems design.

paxdiablo
+1  A: 

Copied from a duplicate for completeness (Nrj):

AOP is all about managing the common functionality (which span across the application, hence cross cutting) within the application such that it is not embedded within the business logic.

Examples to such cross cutting concerns are logging, managing security, transaction management etc.

Frameworks allows this to managed automatically with the help of some configuration files.

paxdiablo
+1  A: 

Copied from a duplicate for completeness (Rohan West):

I currently use Post Sharp, i would read the info from their website. I use it to provide a security around method calls.

"PostSharp is an open platform for the analysis and transformation of .NET assemblies. It comes with PostSharp Laos, a powerful yet simple plug-in that let you develop custom attributes that actually adds behavior of your code. PostSharp Laos is the leading aspect-oriented programming (AOP) solution for the .NET Framework."

paxdiablo
A: 

AOP is a new programming paradigm dealing with this concept. An aspect is a software entity implementing a specific non-functional part of the application.

I think this article is a good place to start with Aspect Oriented Programming: http://www.jaftalks.com/Home/Show/Aspect-Oriented-Programming-AOP

Jaftalks