[Recording] Call This Instead: Intercepting Methods

by Gael Fraiteur on 21 May 2013

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&A

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, architecture 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.