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.
Terminology in AOP can be confusing and sometimes clouds what I believe is a relatively simple concept to use. With that in mind, I'm going to blog about some of the terms that are bandied about in AOP conversations. My hope is that once you see how simple the concepts are, that the intimidating terms will no longer prevent you from giving it a whirl. (Note: I didn't invent any of these terms, so if you think I'm incorrect or explaining it wrong, please chime in--I always welcome good criticism and correction).
Today's term: join point.
A join point is a place in your program's execution.
Yeah, that's pretty much it. Imagine a low-level, detailed flowchart that describes a program. A join point is like one of the lines between two boxes.
Examples of typical join points that are relevant to aspect-oriented programming: a method execution, an exception being thrown, a property or field getting/setting, an MVC controller action being executed. For instance, if you want to write to a log file when there's an exception in the "Index" method, then the join point that you are interested in is "Index throwing an exception".
AOP may sound like this big high-falutin' term that's way too complicated, but guess what: you may already be using AOP and you don't even know it.
Do you using ASP.NET MVC? Ever used an ActionFilter? Congratulations, you have used aspect-oriented programming. If you are using MVC and haven't used an ActionFilter yet, read on, because you may want to start!
Writing an ActionFilter is very similar to writing a PostSharp aspect: you create a new class that inherits from a base class (ActionFilterAttribute), and then you override one or more methods depending on what you want the ActionFilter to do. You apply the aspect to a method or entire class with an attribute. Unlike PostSharp, this form of AOP does not involve a post-compile step. The filtering takes place at runtime, and is invoked by the MVC framework. This means that if you are writing a unit test against the controller action, that test will not trigger the ActionFilter.
I'm going to be blogging some more details about what you can do with each of these, but when you are creating a class that inherits from ActionFilterAttribute, there are 4 methods that you can override 1 or more of:
- OnActionExecuting - runs before the affected action
- OnActionExecuted - runs after the affected action
- OnResultExecuting - runs before the result of the action runs
- OnResultExecuted - runs after the result of the action runs
If you use the [Authorize] attribute in MVC, then you are using an ActionFilter that's been pre-written for you (technically AuthorizeAttribute inherits from FilterAttribute, but it's the same principle). Instead of checking the User.Identity inside every one of your Controller actions, you can just slap the [Authorize] attribute on it and it will keep that cross-cutting concern encapsulated away from the rest of your Controller logic.
There's a whole conference dedicated to aspect-oriented programming. The AOSD (Aspect-Oriented Software Development) conference is put on by the AOSA (Aspect-Oriented Software Association).
The 2012 conference is on March 25-30, in Potsdam, Germany. A five day conference on AOP? Wowza. But wait, there's more. You can also attend FOAL (Foundations of Aspect-Oriented Languages) while you're there in Potsdam on March 26, where Eric Bodden will be keynoting.
All things considered, I'd really like to go, but I definitely can't. Maybe next year decade, after I save up enough vacation days and learn some German?
I started doing some AOP screencasts for dimecasts.net some time ago. I had a roadmap in mind, but due to some unrelated events, I never got around to making the third one of these yet.
The first one is a pretty standard Hello World AOP 101 screencast that uses PostSharp: Getting Started with AOP Using PostSharp.
The second one is called Working With Advanced Aspects, and just expands on some more PostSharp features around boundaries and interception.
I had definitely planned to do 2-3 more of these dimecasts, as sort of a way to give back to a site that helped me to learn some important tools and techniques in an efficient and convenient way.
So, I could continue with that plan, but if you have a suggestion as to what you would want to see in an AOP screencast, I'm all ears. Want to see some runtime AOP with Castle DynamicProxy? Or maybe some validation and initialization techniques with PostSharp? Or something else?