I’ve been off of the AOP bandwagon for some time now. This is why I can’t agree more with Craig Andera’s recent blog entry on Side Effects and Code Generation.
Craig and Tim are old colleagues of mine from my DevelopMentor days. Last year, Tim and I spent some time at together at the PDC talking about Aspect Oriented Programming. I distinctly remembered Tim dissing the prospects of AOP as a general purpose abstraction tool for many of the same reasons that Craig pointed out in his blog entry. At that point in time, I totally agreed with Tim, since my work in that area led me to roughly the same conclusions.
First, a bit of background. Some of you out there know about my prior work with CLAW, my Cross-Language Aspect Weaver for the CLR. I spent the better part of six months of my life thinking about nothing else other than building and implementing a dynamic aspect-weaving engine. Along the way, I learned an awful lot about how to do cool things to impress my friends, like dynamically injecting IL code into running assemblies.
In the early stages of CLAW, I did all of my work in relative isolation. Once I discovered that there was an active AOP community out there, I jumped at an opportunity to present my work at the first ever academic conference on Aspect Oriented Software Development: AOSD 2002. I went to the conference for two reasons: one was to present my work for the purposes of feedback, and the second was to see how people were applying AOP techniques.
Unfortunately, what I found out was that most of the folks at the conference were still talking about the canonical examples of AOP: tracing, logging, security, and instrumentation. What all of these examples have in common is the fact that the aspects are stateless. When you start thinking about how to build stateful aspects, you run into much of the same problems that web developers run into when building web pages: how do I manage the caller’s context?1
Building stateful aspects requires the ability to manage call context. If you can successfully associate call context with thread id’s, for example, everything works great. But if you’re in an environment like ASP.NET, where you can’t associate call context with thread id’s (since the thread pool is managed by the ASP.NET runtime and not you) you run into a much harder problem. The problem arises because you don’t control both ends of the call: you only control the “server” side of the call.2
Complicating matters is the composition problem that Craig points out: services are not generally orthogonal. The problem can be boiled down to who gets called first? Aspects must be called in some order, and some orders will yield correct results, whereas other orders do not. This is even more important in the case of aspects that yield side effects, such as committing a transaction.
All is not lost on the AOP front, however. I think that AOP is yet another tool in your toolbox that you can use to solve specific problems. Most of those problems fall into the side-effect-free category, such as tracing, logging, and instrumentation. There are really useful applications of AOP in these areas, particularly in the instrumentation arena. It can be very useful in cases where you need help to diagnose problems on running production servers. For an example of work in this area, check out the work that IBM Research has done using HyperProbe.3
Authorization checking is an example of a stateless aspect that yields a side effect. AOP can be used to implement authorization checks, but you run into the problem of defining what the interactions between it and other stateful aspects are. I know that there are some companies out there working on using AOP as a technique for implementing security in existing systems. I suspect that they will succeed as long as they don’t have to fight with other services for the same join points in the system.
Craig ends his entry by singling out code generation as the possible answer to the problems that AOP tries to solve. One way of looking at AOP is to think about it as being yet another code generation tool, which it is. The problem is that most AOP implementations tie aspects to a particular implementation language: AspectJ is a perfect example of this.
However, I have a different hope for code generation: one of being able to build technology-neutral abstractions. You create a domain language that models a particular problem domain, and you write code generators that target specific implementation technologies. The real power here is being able to evolve your abstraction independent of the underlying implementation technology. For example, you can build an abstraction that represents an object-relational mapping layer that works with SQL Server and Oracle. This is an example of a category of problems that I call cross-technology abstractions.
Inventing domain languages from scratch is a hard problem. But my personal goal is to make it easier for you to make this happen in your own projects by cataloging patterns that appear in certain problem domains. I’m currently hard at work building a code-generated version of Pet Shop 3.0. Creating a code generated implementation is the easy part; finding patterns that stand up to scrutiny is another matter entirely… Stay tuned for more details!
[1] Web developers like to think about this as the caller’s session.
2 Defining join-points on the caller side of the equation is much harder than it looks. You run into context problems here as well – and the nasty thing is that the context changes whenever a dev touches that client code.
3 I saw Doug Kimelman’s excellent presentation on HyperProbe at AOSD 2002. Unfortunately, I haven’t seen much discussion of it on the web since then.
7 archived reader comments
Jose Luis Sampayo (2003-06-12)
That sounds really great. Could you share with us a bit of that code? It would be nice. Thanks a lot.
Ken Cowan (2003-06-13)
Hi John,
“one of being able to build technology-neutral abstractions. You create a domain language that models a particular problem domain, and you write code generators that target specific implementation technologies”
Have you seen OMG’s Model-Driven Architecture (MDA) work? http://www.omg.org/mda
MDA defines Platform Independent Models (PIMs) and Platform Specific Models (PSMs). The idea is that the business analyst can model the business process as a PIM. A technology wonk defines patterns that transform the PIM into a PSM. An application architect customizes the PSM to handle specializations that aren’t in the pattern. The technology wonk also defines patterns that transform the PSM into working code. A developer then customizes the code as necessary, such as making it conform to your company’s standard look and feel.
In the case of our OptimalJ tool, we give you a bunch of patterns. You can add your own as well if you wish. Patterns are a good place to capture boiler plate code.
KC
John Lam (2003-06-13)
Thanks for the heads-up on the MDA stuff, Ken! It was on my list of things to do, but I had initially dismissed it as “yet another UML modelling tool”. I’m going to look into it on a lot more detail this weekend as I plan on curling up with at least one MDA book :)
Gunnar Skogsholm (2003-06-14)
Ahh, an issue close to my heart:
Craig: >> a whole app in zero lines of code being the ultimate experience. Obviously I�m exaggerating, but I think the phenomenon of seeking to do things with fewer and fewer lines of code is a real one<<
Although I didn’t get on the AOP bandwagon, I do have strong feelings about code generation. I hate it! Our motto at Couloir is: If you know enough to generate code, you know enough NOT to. In fact, at Couloir, we have managed to create significant applications without ANY source code.
John Lam (2003-07-01)
But then how do you debug that code, Gunnar?
Pieter Van Gorp (2004-01-08)
Best regards, Pieter Van Gorp.
Jimmy Nilsson (2004-02-05)
Hi John,
Sorry for a very late comment, but… :-)
Anyway, here it is: http://www.jnsk.se/weblog/posts/aop.htm