1

Possible Duplicate:
What is aspect-oriented programming?

I have been reading up on aspect-oriented programming. I can't find any resource that can explain in layman's terms the concept of AOP. In particular, I'm having difficulty understanding what an "advice" is and what exactly a weaver does. If anyone could explain or have a good resource on AOP, that'd be awesome.

Community
  • 1
  • 1
rmp2150
  • 777
  • 1
  • 11
  • 22

2 Answers2

2

Any introductory tutorial should be fine; what specifically aren't you understanding about the terms?

A "weaver" handles the modification if the original code to include the aspects. For example, in Java, byte code is manipulated at compile or load time to inject code that modifies the original flow. "Advice" is the injected code.

(Roughly.)

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Thanks a lot. The tutorials I found online had high-level language that I couldn't understand. Thanks for simplifying it. – rmp2150 Oct 04 '11 at 05:00
  • @rmp2150 No problem-AOP can be pretty interesting, but without experience, going past the canonical examples (which are still very useful!) can be a challenge :) – Dave Newton Oct 04 '11 at 05:13
1

Aspect-Oriented Programming is used when you have a "Cross-Cutting Concern," something that affects the whole program, and can't be neatly boxed up into an ordinary class.

The canonical example of AOP is logging. Your entire application needs logging, but you don't want to new up a logger object every time you want to log something in a class, because that tightly ties your classes to a logger object, and scatters logging code throughout your application.

Instead, what you want to do is have a logger class (perhaps a static one, so you don't have to instantiate it), and then have some way to call that class from anywhere. You want to keep it loosely-coupled, so that you can swap out the logger class for a different one, if you want to.

There are a number of ways to call a class via AOP. In C#, one way to do it is by using Attributes:

[MyLogger()]
CallingThisMethodCallsTheLogger(int someParameter)
{
    // Do something with someParameter that has nothing to do with the logger
}

Another way to call class methods via AOP is to instrument them. This can be done by performing binary rewrite of the class methods, so that they call the AOP method first, and then call the usual method in the usual way. Instrumenting classes could be done, for example, to measure their performance.

Standard terminology used in Aspect-oriented programming may include:

  • 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.

  • 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.

http://en.wikipedia.org/wiki/Aspect-oriented_programming

Community
  • 1
  • 1
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • That example sounds more like DI/IoC the way it's stated-AOP logging has more to do with eliminating logging from mainline code altogether, not the ability to swap out implementations our avoid mainline instantiation, which DI already allows. – Dave Newton Oct 04 '11 at 04:59
  • @Dave: Nevertheless, those are indeed nice benefits of AOP, and you don't need a DI framework to achieve them. – Robert Harvey Oct 04 '11 at 05:03
  • Sure, you just need an AOP mechanism. – Dave Newton Oct 04 '11 at 05:06