PostSharp: filters for C#
Aspect oriented programming is a relatively new paradigm but it’s existence in the the .NET world was brand new to me when one of my clients/bosses Kris Kniaz stopped by my desk and told me about it. He pointed me at their site and at first it was a little vague what the software was trying to accomplish but about 2 minutes into reading it over I realized I was looking at C# filtering!
So what do I mean by c# filtering? In Rails, when you want to run a specific method before, after, around another method you use a filter. This makes for some very DRY code when you throw in the ability to white list and black list methods from the filters.
The obvious and oft-cited example in Rails is authentication:
For the destroy method in SomeController, before its executed, authenticate will run, which might makes sense! Note that the :only option can be replaced with :except option to black list or it can be left off all together.
So in the case of PostSharp you would end up with something like this:
Its not quite as DRY as The Rails Way, but its a pretty awesome tool to have in your kit!
Obviously this has some draw backs, in the same way it does with Rails. If your programmers aren’t smart they are going to overlooking a lot of possible code executing when they see those attributes. In general it can just lead to a lot of unintended branch logic, but good documentation/communication among your team, and using this powerful tool in the right scenario could result in some really clean solutions.
I encourage you to take a look at the PostSharp demo video, and comment if you’ve used it before!
No Comments Yet