Last updated : 09/11/2002; 12:44:40

Joe's Jelly

Joe Walnes rambling on and on about Software Development, Java and Extreme Programming.

NAVIGATION >>

/WIKI /WEBLOG /THOUGHTWORKS /OPENSYMPHONY /EMAIL

:: ELSEWHERE
xp developer wiki
c2 xp wiki
the server side
javaworld
sd magazine
jakarta

:: BLOGS
Ara Abrahamian
Mathias Boegart
Mike Cannon-Brookes
Paul Hammant
Aslak Hellesøy
Darren Hobbs
Patrick Lightbody
Charles Miller
Brett Morgan
Rickard Öberg
Joseph Ottinger
Mike Roberts
Chris Stevenson
James Strachan

:: INVOLVEMENTS
SiteMesh
QDox
MockDoclet
OSCore
OSUser
PropertySet
RichTags
Marathon
SiteMesh C++
Alt-RMI
Jelly MockTags
more...
:: BACKLOGS
October 2002
Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Sep   Nov



Click here to visit the Radio UserLand website.

Click to see the XML version of this web page.

joe@truemesh.com

:. 20 October 2002

  10:24:22 AM  

AOP in CLR. Came across an article about implementing AOP in .Net CLR. Very intersting approach imho. Something between AspectJ's relatively static approach and Rickard's relatively dynamic and configurable approach. Rickard, what do you think about it? [Memory Dump]

This approach to AOP in .NET seems a bit overkill. After reading what Rickard was doing with AOP whilst stuck on a .NET project, I found it hard to resist creating a little AOP framework. Here's an example of usage...

Take this class:

[Aspectable]
public class Thing extends ContextBound {
// Annoying constraint:
// Classes need to extend ContextBound and have Aspectable attribute.
  public void DoStuff(string name) {
    Console.WriteLine("Hello " + name);
  }
  public static void Main(string[] args) {
// command line entry point


    Thing t = new Thing();
    t.DoStuff("Joe");
  }
 
}

Which outputs:

Hello Joe

To add an aspect, just place create a class like this, add to assembly and it'll automagically do its stuff:

[Aspect]
public class MyAspect {
  [Pre, Match("Thing.*")]
  public void FigureOutFullName(CutPoint cut) {
    string name = (string)cut["name"];
    if (name == "Joe") {
      cut["name"] = "Joe Walnes";
    }
  }
 
  [Post, Match("Thing.*")]
  public void SayBye(CutPoint cut) {
    Console.WriteLine("Bye " + cut["name"]);
  }
 
}

Now the original invocation outputs:

Hello Joe Walnes
Bye Joe Walnes

Aspects can be invoked before and after method/constructor/property calls specified with by full-name, wildcard or regexp. The CutPoint class provides a context allowing the parameters, return type or thrown exception to be modified and optionally to cancel the call to the real method. Multiple aspects can be applied to a single call and priorities can be specified. The aspects can be modified at runtime.

The implementation was pretty simple (four small classes) and I unfortunately never got a chance to use it on my project (which has now finished) - but maybe the next one. Strangely aspects are not built into the CLR, but nearly all the features to create them are there - shame MS didn't standardize on this.

Now all I have to do is tap into Rickard's brain to figure out how to make a killer app :).


Web-design by John Doe from open source web design.