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?
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?
Security - checking that users have appropriate permissions prior to executing certain methods.
Public invariant checking. Since PostSharp 1.5 will come with aspect inheritance, even through interfaces, it will give a lot of new opportunities.
Security
Friendlier error msgs for asp.net webcontrols/webparts
Performance
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; }
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:
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
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.
This blog article by Adrian Colyer is an attempt at explaining AOP without the buzzwords, including several examples that are not logging.
My photo album uses aspectj for three things:
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.
Another classic example (like logging) is caching. But other examples are more interesting.
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.
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.
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