Posts tagged with 'aspect'
I continue to try and cut through some of the confusion around AOP by providing simple definitions and examples of often obtuse terminology (see also: other terminology posts).
"Aspect" seems like the core term in "aspect oriented programming" doesn't it? An aspect is simply a way to indicate to an AOP tool: what and where. The "what" is the advice, and the "where" are join points.
Let's review the terms covered so far by looking at a very trivial example of AOP:
In that example:
- The join point is "OnEntry", i.e. before a method is executed
- The advice is the body of the OnEntry method (Logger.Write(...))
- The aspect is the MyAspectAttribute class itself
- The pointcut is a combination of the applied [MyAspect] attribute along with the join point
- i.e. the complete pointcut is "before the MyMethod method of MyClass is executed"
- in this illustration, it's a very specific pointcut, but useful real-world pointcuts would be broader
- The cross-cutting concern is logging (haven't gotten to that term yet; what's the rush, it's only the name of the site!)
- The weaving is performed by the PostSharp post-compiler (also haven't gotten to that term yet)
Looks like two more terminology posts to go. If there's a term that I'm missing or glossing over, let me know, and I'll write a post on it.
It's been a pretty light week as far as interesting new links popping up. But here are three interesting items to check out. Have a good weekend!
- The English in this white paper is a little rough, but I think they are using AspectJ to help find race conditions in multi-threaded programming. [PDF]
- This is an older post, but still relevant: Myths and realities about AOP
- Another AOP tool for .NET, called SNAP (Simple .Net Aspect-oriented Programming), and here's the Github repo for SNAP - it looks to be a tool that sits on top of Castle DynamicProxy and integrates with your favorite IoC container.
- A white paper from Germany on analyzing models to identify cross-cutting concerns, to best apply AOP when designing an application [PDF]
Recently I was made aware of another AOP tool for .NET that uses a post-compile step to do the weaving (similar to PostSharp). SheepAspect appears to use the Mono.Cecil library, which is a powerful utility to work with CIL assemblies. I mentioned SheepAspect in an earlier post, and I thought it deserved a closer look.
SheepAspect is available via NuGet, so it's an easy install. Here's a 'Hello, World' aspect in a console app that I threw together:
It's a somewhat unfamiliar approach to me, being used to PostSharp.
- To define an aspect, create a class and put the [Aspect] attribute on it. So far so good.
- You don't use attributes to apply the aspect to your code. Notice that MyClass and DoStuff are not decorated with any attributes.
- Instead, you define a pointcut by creating an empty "marker" method (MyPointcut in the above example), and placing a selector attribute on that. The one I'm using is SelectMethods, but there are others available.
- The string in that SelectMethods attribute is called SheepAop Query Language (SAQL).
- The advice goes into the InterceptMethod method, which returns an object and gets an MethodJointPoint (yes MethodJointPoint, not MethodJoinPoint, that's not a typo). MethodJointPoint is very similar to DynamicProxy's IIntercept or PostSharp's MethodInterceptionArgs, in that it gives you a context to work with.
- I put an "Around" attribute on this method to specify that I want this advice to surround the pointcut, and I specified the pointcut with the string.
Compile and run, and here's the output:
After using SheepAspect in this admittedly trivial way, I do think it has some potential, but it also feels like a project that's in its very early stages and is a little rough around the edges still.
- SAQL is a very interesting way to select pointcuts, and based on the SAQL documentation that I've read, it can be very powerful. This is a double-edged sword though: this query language is specific to SheepAspect, so you'll have to spend some time learning it to get the most use out of it.
- SAQL queries are just strings, so refactoring/renaming could be problematic. Perhaps if you are coming from an AspectJ environment, this would not be as difficult, since that's what SheepAspect seems to modeled on. A Linq provider would be tremendous, but that might be very ambitious, if not impossible.
- The fact that SheepAspects are only connected to your regular code by SAQL is also an interesting approach: your pre-compile source code goes completely untouched, which is nice and clean, but it could also be hard to keep track of what aspects are being applied where.
- Finally, while there is some good documentation, it looks like some of it has yet to be written, which indicates to me that it's still very much a work in progress.
I can't say I'd recommend using SheepAspect over more mature and supported tools like PostSharp or Castle DynamicProxy, but it might be worth a look for specialized usage in low risk situations. In the future, who knows; it could develop into a very popular tool.
This is the first "Weekly Concerns" (thank you Jason Karns for the name) post, of what is going to be a weekly series of blog posts that's basically just a Friday link dump. Not that each of these links don't deserve more attention and research, but, hey, I get lazy sometimes, alright!
- SharpCrafters webinar - "How to Stay DRY with AOP and PostSharp", featuring PostSharp developer Igal Tabachnik
- White paper from Cornell about using AOP in seperating concerns (available in PDF and other formats)
- The NDC conference in 2011 had an "AOP & IoC" track. I'm guessing many of you didn't make it to Norway for this conference (I didn't), but the NDC is kind enough to make video of the sessions available. Day 3, Track 5 features Gael Fraiteur (creator of PostSharp), Donald Belcham (fellow PostSharp MVP), and some other sessions about the more general topic of rewriting IL.
- SheepAspect for .NET - another AOP framework that uses IL rewriting. I've not heard of this one much, but it is open source and probably deserves a closer look (perhaps in a future blog post).
- AOP for Perl (yes, Perl!) with Aspect from Adam Kennedy (who has his own Wikipedia page), with a very comprehensive and well written README that's a pretty good primer on AOP in general.