Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
Didn't saw that other question before answering. The first answer make a very valid point, and is probably better than mine. –  deadalnix Mar 2 '12 at 14:31
add comment

marked as duplicate by Doc Brown, gnat, Walter, Robert Harvey, Yannis Rizos Mar 3 '12 at 10:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

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.

share|improve this answer
    
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. –  acidzombie24 Mar 3 '12 at 7:00
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.