Common approach for property fallback values

Fallback values for content type properties are often implemented by overriding property getters and setters. While this is an effective way of implementing specialized logic for retrieving the fallback value, it’s a bit cumbersome, verbose and repetitive for many of the more trivial cases:

How it works

EPiServer intercepts all getters and setters of content type properties using an instance implementing IInterceptor, part of the Castle Project. The default interceptor in EPiServer is called ContentDataInterceptor, but using EPiServer’s IoC container we can easily replace this with a custom interceptor which supports our Fallback attribute.

Switch concrete implementation to a custom interceptor

One way to switch concrete implementations in EPiServer is to create a class implementing IConfigurableModule, an interface very similar to IInitializableModule with the addition of a ConfigureContainer method:

Implementing the custom interceptor

This is where the magic happens. We inherit ContentDataInterceptor and add logic which uses the Fallback attribute to determine how a fallback value should be retrieved. This code is fairly lengthy, but the comments hopefully clarify what is going on: