Chapter 9. Aspect composition: example and execution

 

This chapter covers

  • The ordering of aspects being applied to the same code
  • Using aspect roles in PostSharp to specify order/dependency
  • Composing the order of interception aspects with DynamicProxy
  • Real-world example of composing caching and authorization aspects

As you continue to use AOP, you’ll want to apply multiple aspects to the same pieces of code, and this chapter covers how to compose aspects together in a predictable way. If you’re new to AOP, it’s best to start small until you get the hang of it. Introduce a simple aspect (like logging) to your project and see how it goes. For me, it went well, so I wanted to start creating more aspects to handle more cross-cutting concerns. At some point, though, I’ll run into overlap in the code that aspects get applied to. In chapter 2, we wrote aspects for multiple cross-cutting concerns, all on the same piece of code, repeated here in this listing.

Listing 9.1. Multiple aspects on the same method

I briefly mentioned that ordering was a concern, because the order that C# attributes are applied to code is non deterministic (that is, the order you put attributes on code isn’t necessarily maintained by the C# compiler).

9.1. Using multiple aspects

9.2. Aspect roles with PostSharp

9.3. Composing aspects with DynamicProxy

9.4. Real-world example: caching and authorization

9.5. Summary