Posts tagged with 'SheepAspect'

SheepAspect: a closer look

February 09, 2012 mgroves 0 Comments
Tags: SheepAspect Cecil

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:

Sheep Aspect example console 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.
Matthew D. Groves

About the Author

Matthew D. Groves lives in Central Ohio. He works remotely, loves to code, and is a Microsoft MVP.

Latest Comments

Twitter