Matthew Groves delivered part 3 of his 5-part live webinar series on AOP in .NET recently. The series covers insights found in his new book and a variety of real-world coding examples using PostSharp.

In part 3, Matt looks at a real-world application of a method boundary aspect to help with caching.– followed by a Q&A session.


Q: Is OnExit always executed and, if yes, is it before or after OnSucess or OnException?

A: Imagine an invisible Try, Catch and Finally surrounding your method, then picture OnExit as being inside the Finally block of that imaginary Try/Catch block.

I say imaginary but, in the next episode, we will look at decompile and assembly and you’ll see there is a real Try, Catch and Finally block being generated by PostSharp. So, OnExit will run after OnSuccess because the code and Finally runs after the code inside the Try block.

Q: In the cache example is the data saved in the cache again, and is OnSucess even called or is it skipped because of the FlowBehavior?

A: When we set FlowBehavior to return that means OnSuccess, OnExit and OnException won’t be called at all. It will just return immediately and those boundaries won’t be executed, and it won’t be stored in the cache again.

Q: Instead of using a Cache object instantiated inside the aspect, is it possible to inject an implementation of it?

A: In my example I tightly coupled an aspect to a specific caching framework [ASP.NET cache]. If you’re suggesting I should inject a dependency so I’m working against an interface instead of directly against the ASP.NET cache, I agree with you.  We’ll talk more about that in the upcoming unit testing episode, because an important part of unit testing is not having those elements be tightly coupled.

Q: How do we refresh the cache? Say if the value we are pulling from DB (say Median income) is changing every 10-15 minutes how can I keep my data up to date?

A: The answer depends on the caching framework you’re using. With ASP.NET cache it has a default expiration of around 20 minutes. You can change that if you’d like. In my example I set the cacheKey directly by writing:

HttpContext.Current.Cache[cacheKey] = args.ReturnValue 

You can also use the Add method:


Here I can add the key, the value, CacheDependency, absoluteExpiration time, slidingExpiration time, Priority, etc. So if the median income caches often, you may want to set it to expire more frequently than every 20 minutes.

Q: Are there some useful pre-built caching aspects in PostSharp?

A: PostSharp 3 currently offers ready-made aspects to handle logging, INotifyPropertyChanged and multithreading. Ready-made caching is on the horizon. In the meantime, you may want to look at Attribute Based Caching, found on CodePlex, which uses PostSharp to handle many of the implementation practices talked about today.

Q: In your upcoming book, will the examples/code work in PostSharp 2.1 or is the latest version 3 required?

A: When I started writing the book PostSharp 3 wasn’t released yet so I mostly used version 2.1 to write the code. The coding examples however should work fine in either version.

Up next: Matt Groves goes Under the Hood of a Post-Compiler, showing how to decompile a .NET assembly and view code with and without a PostSharp aspect with Reflector.


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.