Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

The More the Merrier


The More the Merrier

At his July 2004 breakfast meeting with software architects in Sydney, Australia, Bill Gates announced Microsoft's plans to embrace aspect-oriented programming. The news has pessimists worrying that Microsoft might seek to redefine AOP as only about attributes, a technology Microsoft introduced in C#.

But I think that won't happen, for a few reasons. First, AOP and attributes are synergistic. Attribute-enabled AOP is more powerful than either alone. On the Java platform, support for attributes will shortly be added to all the AOP languages. It would benefit .NET programmers to have that same functionality. (It's no surprise that AOP and attributes are synergistic—developers of AOP built aspects with that in mind.)

Also, AOP is no threat to Microsoft. As I noted last month, AOP can be used as a tool to make platforms more useful and valuable, provided the vendor is clever about the platform-control game. And say what you want about Microsoft, but they're very clever about platform control.

In April, I summarized attributes, explaining that they introduce into C# the basic syntactic hook required to enable Lisp-style macros. Attributes make it possible to tag parts of a program with information that can be used in various ways. Of most interest in this discussion is that tools can use attributes to determine where to extend semantics of the program, by modifying the program or the environment in which it executes. Attributes and AOP solve different modularity problems, however. Attributes provide modularity similar to that of methods, procedures and macros. With methods, wherever you explicitly call the method, you get the behavior of its implementation. Similarly, wherever you explicitly type the attribute, you can arrange to get additional behavior from the attribute.

AOP advice and introduction have a different kind of modularity. They can reach out and add behavior, even to join points that don't request it explicitly.

AOP Enhances Attributes

By default, attributes are just syntax. There's no standard way to associate runtime behavior with an attribute, so people sometimes use low-level and too-powerful mechanisms such as bytecode manipulation to add attribute behavior.

AOP provides a great mechanism for associating behavior with attributes. Once AOP pointcuts are attribute-aware, you can use AOP advice to define the behavior that should be associated with an attribute:

 pointcut transaction(): call([Transaction] * *(..));<br>
 
 around(): transaction() {
   <i>appropriate code around
   call to proceed</i> }
Attributes Enhance AOP

A common complaint about AOP is that pointcuts that rely on naming conventions may be dangerous. Some worry that the name pattern will match unintended join points.

Attributes will let you replace pointcuts that rely on naming conventions with pointcuts that use attributes. So instead of writing execution(void com.bigboxco..*.set*(..)), you just tag each setter method with a Setter attribute, and the pointcut becomes execution([Setter] * *.*(..)). Now only methods that explicitly have a setter tag will match the pointcut.

Because AOP pointcuts are compositional, you can rely on the setter naming convention by default and use attributes to tag exceptions to the rule. Then you write a pointcut like:

 pointcut setter(): 
     execution(![NotSetter] void *.set*(..));
And you tag only those methods that match the naming convention but are not in fact setters. An AOP-aware IDE will help you figure out what methods to tag, by constantly showing you what methods match the pointcut. (The pointcut syntax says, "Execution of methods that don't have the NotSetter attribute, return void, and whose name begins with 'set'.)

When should you use attributes instead of non-attribute pointcuts? Use attributes when join points need to be identified based on a property that requires human insight to decide. Non-attribute-based pointcuts are good when the property can be determined by computer.

Attribute names should describe the join points being tagged, rather than the advice that will apply. This makes it natural for multiple aspects to add advice to join points with specific tags. For example, tagging a field as [SensitiveInformation] might be useful for encryption, authentication and auditing aspects. [ReadOnly] is a good name for an attribute; [ReadLock] ties too closely to specific behavior. Similarly, you can tag in terms of business concepts and then advise those in a variety of contexts, from publishing business events to handling security or managing persistence and transactions.

Once attributes are embedded into an AOP framework, all the AOP power can work with them. For example, using the popular cflow pointcut, it's possible to distinguish between top-level and nested transactions:

pointcut topLevelTransaction():
     transaction() && !cflowbelow(transaction());
 pointcut nestedTransaction():
     transaction() && cflowbelow(transaction());
If you have only attributes without an AOP framework, it's difficult to achieve this effect. You'll end up writing your own mini-pointcut language.

Here, I'm just showing examples of using attributes with advice, but you can also use them with introduction. For a familiar example, you might decide to explicitly tag Subject and Observer classes. Having done that, an aspect could add add/remove observer methods using a syntax similar to this:

 void [Subject]*.addObserver([Observer] obs) { ... }
Extending AOP systems to understand attributes will provide immediate benefits: a way of associating behavior with attributes and of addressing the "fragile pointcut problem. Using a mix of attributes and non-attributes to identify join points is extremely powerful, especially when it's combined with the kinds of pointcut languages that AOP provides.

Microsoft working with AOP will also be great. Today, AOP languages for .NET are less mature than those for Java simply because the initial research and development was almost entirely Java-based. But we now have a pretty good idea of what AOP should be, and the developers of the Java AOP languages have started work on integrating AOP and attributes. It's a good time for serious work to begin on AOP for .NET.


Vancouver-based consultant Gregor Kiczales led the Xerox PARC teams that developed AOP and AspectJ.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.