Aspect-oriented programming examples

Viewed 16654

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?

14 Answers

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:

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

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; }

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.

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

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

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.

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

Related