A Short Review of PostSharp

By January 7, 2014 February 27th, 2016 General

If you do a lot of .NET development, then you shouldn’t be without PostSharp—the “swiss army knife” of .NET design patterns. Since we recently wrote some documentation for this amazing .NET development library we thought we’d share a brief review of some of its features.

First off, what is PostSharp? In a nutshell it’s a set of common design patterns which have been encapsulated and automated for easy integration into your .NET projects. Let’s take a quick look at three examples which illustrate just how cool this library is.

First up is method and property interception. Let’s say for example that you want to write some verification code which checks an input parameter or the incoming value during a property “set” operation. Moreover, you want to use this same verification code for a large number of properties and/or methods. Normally you would either duplicate this code or write a method to do this and then call this method from every point where the verification should be performed.

What PostSharp allows you to do is create this in an attribute class, and then apply this attribute to every property and method where this check should be performed. PostSharp then “injects” this code at build time, which will be invoked before the property or method is executed at runtime, to perform this verification. With this code you could either modify the value or throw an exception—either way you now have an encapsulated verification process which is dead simple to apply as an attribute.

Another cool feature is logging which is as simple as adding a Log attribute and selecting the backend to use for performing the logging procedure. What’s cool about this is that the Log attribute automatically picks up the names of parameters (even if they change between builds) and includes this information in the build messages. Moreover, the logging system supports Log4Net, NLog, Enterprise Library as well as System.Diagnostics.Trace, and System.Console, and automatically adds the dependency assemblies. These can also be swapped at anytime by modifying the underlying project file.

The final feature that we’ll mention is PostSharp’s threading system which features a number of threading patterns which simplify and automate concurrency. One such system is the ReaderWriterSync attribute.

Normally in threading you have to deal with cumbersome methods for defining critical sections and handling locking and unlocking of resources. Not only is it difficult to add such control points, but doing so can also create high contention for resources resulting in poor threading performance or even deadlocks. And as most multi-threading programmers will attest to, threading bugs can be difficult if not impossible to debug.

PostSharp automates and simplified this through its ReadeWriterSyncronized attribute. Simply add this attribute to a class, then add a WriterLock attribute to all methods or properties which need to modify a synchronized resource, and a ReaderLock attribute to all methods or properties which need to read a synchronized resource. With this system not only are all read and write operations automatically locked until completion, but any reads or writes which are not marked with WriterLock/ReaderLock will generate an exception, allowing you to quickly identify code which is not marked for synchronization. This system also eliminates the need to wrap accesses to resources in “enter” and “exit” statements which convolute code.

So if you write .NET code, be sure to check out PostSharp as it could help to automate and simplify many common design patterns that you’re using: http://www.postsharp.net/.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.