tags:

views:

1155

answers:

13

Can anyone post an example of Aspect-oriented programming (AOP) that is not logging?

I've looked at several resources but all the examples are trivial logging. What is it useful for?

A: 

Security - checking that users have appropriate permissions prior to executing certain methods.

Dan Vinton
A: 

Public invariant checking. Since PostSharp 1.5 will come with aspect inheritance, even through interfaces, it will give a lot of new opportunities.

Lasse V. Karlsen
+4  A: 

Security

  • Inject code that checks permissions and blocks access

Friendlier error msgs for asp.net webcontrols/webparts

Performance

  • Inject code that sets perf counters to get an overview of where your app is slow
AndreasKnudsen
+4  A: 

Validation:

[NotNull]
public string Property1 { get; set; }

[Length(Min = 10, Max = 20)]
public string Property2 { get; set; }

[Regex(Expression = @"[abc]{2}")]
public string Property3 { get; set; }
Paco
Where is de Aspect?But you could use some advice to read this "annotations" that come from a joinpoint provided by a pointcute like:* * save(..)Thus you can use a before advice to validate and proceed if so or throwing some fail conditions about the invalid state.
paulosuzart
It depends on where the validation is used and what kind of UI framework is used. I don't use the aspects on entities that needs to be validated, but more for "defensive coding". I do not use the validation of entities on a save method in a repository, but I validate somewhere in the UI.
Paco
+5  A: 

One of the examples which was loaned straight from this Aspect Oriented Programming: Radical Research in Modularity, Youtube video was painting to a display. In the example you have a drawing program, which consists of points, shapes, etc and when changes to those objects occur you need to tell the display to update itself. Without handling it in an aspect you end up repeating yourself quite a bit.

AOP, as I've understood it, was created for not repeating yourself for cross cutting concerns which might not have anything to do with business logic. With aspects you can modularize these concerns to aspects. One of the examples was logging but there are bunch of different things you might end up repeating. It has been evolving since and it's not any more about aspect-oriented programming but there's also aspect-oriented modelling.

More information about aspect oriented programming can be found from these sources:

Bleadof
+2  A: 

Design Pattern Implementation in Java and AspectJ (Hannemann and Kiczales): http://www.cs.ubc.ca/labs/spl/papers/2002/oopsla02-patterns.pdf

The paper shows how some of the GoF design patterns can implemented in a better way in Java using AspectJ

Eyvind
+1  A: 

You cannot have multiple inheritance in Java. However by using AOP you can have "limited" multiple inheritance. Try to google this to see some examples.

I also agree with Eyvid. Hannemann and Kiczales paper is great for learning the basics on design patterns and getting some great AOP examples.

Chrys
+4  A: 

This blog article by Adrian Colyer is an attempt at explaining AOP without the buzzwords, including several examples that are not logging.

Jörg W Mittag
+1  A: 

My photo album uses aspectj for three things:

  1. Implementing the observer "pattern" as a reusable piece of code.
  2. Destroying a session for a specific type of call.
  3. Setting dirty flags on DTOs when mutation occurs.

The first, in particular was pretty much straight out of a google tech talk on AOP. It's about modularity in a different way than most people consider. Definitely recommend watching that if you're interested in how to use it for good.

Dustin
+1  A: 

Another classic example (like logging) is caching. But other examples are more interesting.

orip
+1  A: 

Undo - I am calling a third-party assembly that supports undo operations. It requires callers to create an undo context, call some methods in the assembly, adn then destroy the undo context. Contexts can be nested. Also, if a context is created but left in an undesirable state that requires an app restart.

Normally to use undo I would write something like this

    void foo()
    {
        int id = lib.create_undo_context();
        try
        {
            lib.performsomeaction();
            lib.performsomeaction();
            lib.performsomeaction();

        }
        finally
        {
            lib.destroy_undo_context(id);
        }
    }

with PostSharp I define an attribute called [Undo] that creates the undo context when the method starts and destroys it when the method exits (even if an exception is thrown) - so the code looks like this

    [Undo]
    void foo()
    {
        lib.performsomeaction();
        lib.performsomeaction();
        lib.performsomeaction();
    }

It's a little more complicated to implement this than I am showing because I have ensure that all undo contexts are cleaned up even in cases where there are nested Undo contexts - but you get the idea.

namenlos
A: 

Transaction management.

To my mind, you do not want objects that may be part of a transaction to be aware that they are in a transaction. Using AOP, you can compose objects into transactions as required without the objects in the transaction needing to be aware of either the fact that they are in a transaction or even of the existence of the AOP framework.

Alfamale
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