Chapter 3. Call this instead: intercepting methods
This chapter covers
- What method interception means
- Using Castle DynamicProxy to intercept methods
- Writing a data transaction aspect
- Using PostSharp to intercept methods
- Writing a threading aspect
In chapter one, I talked about join points and pointcuts. I left the definition of these terms fairly broad, defining a join point as any point in between your code and describing a pointcut as a collection of join points. I kept these definitions loose because aspects can theoretically be applied to any location in your code that you can describe. You could put an aspect inside of an if statement or use it to modify a for loop. But you don’t need to do that 99% of the time in practical application.
Good frameworks (like PostSharp and Castle DynamicProxy) make it easy to write aspects using predefined join points and give you limited ability to describe pointcuts. However, you’ll retain enough flexibility to tackle the vast majority of AOP use cases.
What about that 1%?
Low-level tools are available that let you modify or create code all the way down to the instruction level (IL). These tools include Mono.Cecil, the PostSharp SDK, .NET Reflection, and Reflection.Emit. But these tools aren’t the focus of this book. I’m focusing on writing aspects, not on the entire realm of metaprogramming. We’ll revisit some of the low-level tools in chapter 7 and appendix A.