[Recording] Before and After: Boundary Aspects

by Britt King on 27 May 2013

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.