Matthew Groves delivered part 2 of his 5-part live webinar series on AOP in .NET recently to support his upcoming book Aspect Oriented Programming in .NET, out from Manning Publications in June.  The series covers insights found in his book and a variety of real-world coding examples using PostSharp.

In part 2, Matt demonstrates the method interception aspect, and looks at a real-world application of the aspect that can help with data transactions – followed by a lively Q&A session.


Q: With the use of Aspect Interceptors, does the behavior of stepping through and debugging the code in either the Interceptor or calling code change?  If so, how?

A: With PostSharp, debugging works great! When PostSharp rewrites your code with IL manipulation it also makes appropriate changes to the debugging file. Which means, if you set a breakpoint in an aspect or in a method that has an aspect on it, that breakpoint will be hit when expected and you’ll get the full stack with that as well.

Q: Can you put a PostSharp attribute on an aspect’s method?

A: No.

Q: Can we apply interceptors globally to all the methods in a class?

A: Absolutely. You can apply the attribute to the class and by default it will be applied to every method within that class. There are lots of customization available with PostSharp. You can customize how you want the attributes to be applied – even at assembly level.

Q: Is it possible to apply an aspect to DLL methods to which you don’t have the source?

A: In theory yes, but this scenario is not supported so you will be on your own in case of problem.

Q: Can we apply multiple aspects to a given method, and in what order are they executed?

A: In some situations, for instance if you have caching and authorization, the order in which the aspect is applied is very important. PostSharp gives us a few ways to deal with that. The simplest one is called AspectPriority where you specify a number to indicate the order in which aspects are run, and there are more robust features that PostSharp has as well to manage dependencies between aspects and aspect composition.

Q: What are some other examples of practical uses of aspects?

A: Logging, data transactions, caching, INotifyPropertyChanged, exception handling, authorization, validation, etc. In fact, PostSharp Ultimate comes with a whole suite of aspects that have been written for you. It’s a new feature in PostSharp 3 so you don’t have to spend time writing these aspects and can simply drop them in and start using them right away.

Q: Can you add an Attribute to a parent object and have it apply to all child objects?

A: Yes, using aspect inheritance.

Q: Will you show how to TDD aspects in this series, and do you show it in your book?

A: Not TDD specifically, but I have dedicated an entire chapter of my book to unit testing aspects and episode 5 in the series (May 30th) will focus on that.

Q: Does the aspect class name need to end with Attribute, as per .NET convention?

A: No, it’s not necessary. As long as you inherit from the Attribute base class, .NET will recognize it as an Attribute.

Q: Can you use PostSharp in Windows Phone 7 applications?

A: Yes.

Q: What are the best practices for bringing dependencies into aspects?

A: That question will be covered at length during the Unit Testing Aspects episode on May 30th.

Q: When will your book be published?

A: June 14th.

If you want to ask Matt any question you may have about AOP in .NET and/or PostSharp be sure to sign-up for the series and join us every Thursday throughout the month of May.

Last Thursday our good friend Matthew Groves delivered part 1 of his 5-part live webinar series on AOP in .NET. He has a new book coming out next month called Aspect Oriented Programming in .NET from Manning Publications and the series will cover some of the practical AOP insights found in his book and a variety of real-world coding examples using PostSharp.

In part 1, Matt does an excellent job of demonstrating the basics of Aspect-Oriented Programming, including a number of live coding examples using ASP.NET and PostSharp – all in under 30 minutes.


Q: Can you provide any hints on unit testing with aspects involved?

A: We’re going to talk about unit testing aspects – unit testing code that aspects are applied to – in part 5 of this series [May 30th]. In fact, I have dedicated an entire chapter of my book to unit testing aspects.

Q: Is PostSharp the only AOP framework covered in your book?

A: My book is focused on AOP in .NET and PostSharp is a large part of that but I cover other .NET tools like Castle DynamicProxy and the ASP.NET examples [shown in the video]. The idea is that once you learn the principles of AOP, you will be comfortable using tools other than what’s covered in the book. So anything you learn about PostSharp or Castle you can apply to other frameworks as well.

I will be adding new recordings each week but, if you want to catch them live and have the chance to ask Matt any question you may have about AOP in .NET and/or PostSharp be sure to sign-up for the series and join us every Thursday throughout the month of May.

We’re excited to announce that PostSharp 3 RTM is now available for download from Visual Studio Gallery and NuGet. After a successful RC cycle, PostSharp 3 now enters the stable quality band and replaces PostSharp 2.1 as our featured version.

PostSharp 3 marks a huge evolution from previous versions. Designed to deliver more value with less learning, PostSharp 3 is now much more than an awesome framework: using simple smart tags in the Visual Studio code editor, you can add to your code ready-made implementations of some of the most common design patterns. Other improvements include first-class support for Windows Store, Windows Phone and Silverlight apps.

Improved Visual Studio Integration

The single most striking feature of PostSharp 3 is its deeper integration with Visual Studio.

For instance, move the caret to the name of a class or a method. Visual Studio will display a smart tag proposing actions that make sense in the current context: apply a threading model, add logging, implement INotifyPropertyChanged, …


A wizard then guides you through the configuration of the aspect.

The wizard installs all required NuGet packages and generates the code for you – so you don’t have to study documentation to figure out how it works.

Of course, you can still add aspects to your code the old good way.

Ready-Made Pattern Implementations

While talking to customers, we figured out that the Pareto law also applies to our product: 80% of customers use the same top 20% aspects. We thought we would provide more value to our customers by providing ready-made implementations for the most common patterns. The results are our three first pattern libraries:

Windows Store, Windows Phone and Silverlight

In previous versions of PostSharp, support for alien .NET platforms was quite limited. The raise of Windows Phone and Windows Store stressed the importance of supporting new trends, so we decided to invest in providing first-class support for Windows Store, Windows Store and Silverlight.

I’m very pleased that we could this through Portable Class Libraries.

The first challenge was to develop a portable alternative to the system BinaryFormatter and its  [Serializable] attribute. The result is the PortableFormatter class and its [PSerializable] attribute – an interesting piece of software in itself.

Deployment through NuGet and Visual Studio Gallery

When PostSharp 1 was released seven years ago, it seemed mandatory to have a setup program. Over the years, source control and build servers have conquered the world, changing requirements on deployment of development tools.

Despites important limitations, Microsoft’s NuGet and Visual Studio Gallery are becoming the de-facto standards for deployment of development components and their user interface, so we decide to get aligned with Microsoft’s lead.

We are aware of some negative consequences and will be happy to work with customers who may be affected by this choice.

Subscription-Based Maintenance Model

PostSharp 3 marks also the move from a per-version to a per-year maintenance model, as described in a previous blog post. A maintenance subscription is now included as a mandatory part of the product fee. The maintenance subscription not only gives right to major versions, but also to bug fixes.

We believe that part of our failure to deliver support for Visual Studio 2012 in time was due to our per-version revenue model. The new revenue model will allow us to release features as soon as they are needed, irrespective of the current major version number.

For more information regarding licensing and support, please read our FAQ page or contact our sales team.

Tip: Upgrade Before Summer

PostSharp 3 is now stable and it’s a good time to upgrade. During the next 2 months, the team will be ready to answer your questions and address support issues, without any other big project in the pipeline. We may be a bit slower during the vacation period, so it’s a good idea not to delay the upgrade if you can do it now.

PostSharp 3 is a big turn for our company, and so far we are very happy with the feedback and the businesses we received.

Happy PostSharping!