|Number of votes:||0|
A colleague asked me why all the properties that are to be mapped with PageTypeBuilder has to be virtual. This is sort of interesting and everyone that’s using PTB might not be aware of the reason so I thought that that merits a quick post about it.
PTB uses something called Interceptors which are used to (wait for it) intercept calls. The framework PTB uses for this is called DynamicProxy and is part of the Castle fame. The reason the properties have to be virtual is that the DynamicProxy needs to override (in the case of PTB) your properties to be able to run the code you (or Joel in this case) specify in the interception.
A standard property using PTB looks something like this
and yet when calling CurrentPage.Heading it automagically has the value of the property CurrentPage[“Heading”]. This is due to the interception of the property Heading. Since the logic of how to handle the getting and setting of properties is the same for all properties you can use the above code instead of what’s actually happening under the covers which is something like this
So PTB uses interception both to be able to infer which property you’re getting/setting as well as handle the actually getting/setting which stays the same for all properties.
PTB first does some wiring so that the interception is activated for the TypedPageData (see the class TypedPageActivator). Since we’re only interested in intercepting those properties we talked about above PTB uses a generation hook for this
So when you access one of the properties the generated proxy intercepts the call. The actual getting and setting of values is handled by the Intercept method in the class PageTypePropertyInterceptor.
As you can see on line 4 it uses the name of the property to avoid having magic string. It then basically does what the code above that explicitly handled the getting and setting does so that you won’t have to.