Chapter 5. Get this instead: intercepting locations

 

This chapter covers

  • What is a location?
  • Using PostSharp to intercept locations
  • Writing lazy-loading aspects
  • How noisy INotifyPropertyChanged makes your code
  • Using AOP to make INotifyPropertyChanged clean and easy

In chapter 3, I wrote about method interception. In this chapter, we’ll look at a different kind of interception: the interception of locations. I’ll start by reviewing what a location is (field or property), then I’ll show you a couple of examples of real-world aspects that you can use on locations. So far, the focus has been mainly on methods. But locations are an important part of your code and can be susceptible to the same types of repetitive boilerplate as methods.

Location interception is a less common feature of AOP frameworks, so most of the examples in this chapter use PostSharp, a framework that supports locations. Additionally, you’ll look at a specialized AOP tool in this chapter (as opposed to a general AOP framework such as PostSharp) called NotifyPropertyWeaver that provides location-interception capabilities, but only for one narrow feature.

5.1. Location interception

You might not have heard the term location in reference to C#. A field is a location and a property is a location. The following code snippet contains two locations, one field and one property:

5.2. Real-world example: lazy loading

5.3. Real-world example: INotifyPropertyChanged

5.4. Summary