3

Possible Duplicate:
Are certain problems solved more elegantly with AOP?

I don't know much about aspect oriented programming (AOP) but I looked at a few examples and it seems to me more of a debugging tool or patching things. I looked over the examples and I can't think of anywhere I'd use them in -my- code.

Assuming the code has been design right and the language supports it (such as AspectJ) what are real world uses where using AOP is good design and can't be done as easily with traditional well designed classes/code/software?

1
  • Didn't saw that other question before answering. The first answer make a very valid point, and is probably better than mine.
    – deadalnix
    Commented Mar 2, 2012 at 14:31

3 Answers 3

4

AOP solves some problems, but is not useful in all cases.

Typically, AOP provides tools to manage separation of concerns. In many projects, you'll find yourself writing the same code repeatedly. A typical example is checking access:

if(!user.hasAccess(functionality)) throw new SecurityException();
// Functionality implementation.

This is not clean code, because the problem of checking access is spread across the whole code base. It is error prone, which is not what you want in general, but even worse here, because an error is a security flaw.

AOP is definitively useful here, because it will allow you to define security at one place and functionality at another.

You'll find other patterns, like connecting to a database, doing some operation, and disconnecting. You'll have to handle all special cases (SQL error, network error, etc . . .) and be sure you rollback cleanly if something happens and disconnect properly. AOP could also help you here, avoiding the need to have all the error handling boilerplate spread over multiple functionalities.

Another problem is synchronization. As the system gets more and more distributed (multicore, network, cloud, GPGPU, etc . . .) it is of increasing importance.

Definitively, AOP is useful in some cases.

1
  • I understand your answer but i never ran into that situation. I check for AccessType than hasAccess(functionality). I put db code behind an API separate from web or winforms. All my important code does have a using around it (.NET). and i use message passing so i don't really have synchronization problems :/. It still feels like its meant to patch problems. I still cant find a place for it in code i write. But +1 anyways. I don't think anyone will convince me that AOP is good for me.
    – user2528
    Commented Mar 3, 2012 at 7:00
1

A lot of frameworks and products use AOP "under the hood". For instance, it's heavily used in Spring for things like transaction handling and method caching. Hibernate leverages AOP-style dynamic proxying to enable persistent collections.

0

I bet you are using AOP in your code (or have in the past) and you didn't even know it. MVC web frameworks often have filters that are used in a very AOP-like way, and even something like an HttpModule in ASP.NET I would call AOP (I'm not sure what the analog is in the Java world).

Debugging, logging, tracing, are certainly the "killer apps" of AOP, but any time where you have boilerplate or repetitive code that you can't otherwise refactor using normal OO techniques is a great place to consider AOP.

It's not a silver bullet, but it's a great tool to have in your SOLID toolbox.