PageTypeBuilder, classes and is-a vs has-a

A pain-point I think we all can relate to

By using PTB we get the opportunity to work more object-oriented with matters concerning page types. A big win for me initially was to leverage inheritance and to define properties such as meta keywords / meta descriptions on a base class and just define those once. When you move to a more complex page structure it can however turn quite messy. You end up with pages that are more or less a random combination “Oh, this page needs the contact info, the right bar and the footer while this page needs just the right bar and the footer and this page…”. Since you can’t have multiple inheritance (and that’s a good thing imho) you end up with class explosion where you need to create any number of odd combinations or just settle for defining certain properties in multiple places.

These situations tend to stem from the fact that pages often used the properties in a HAS-A way than IS-A. So the StandardPage HAS contact info, HAS right column more than IS a contact page and IS a right column page. This boils down to favoring composition over inheritance, something that has been hard using PageTypeBuilder. Until now. Or well, until soon (= when Joel feels comfortable enough with the functionality to release it).

PageTypePropertyGroup

In the master branch of PageTypeBuilder Joel has now merged a brilliant contribution from @croweman and @ev2000 that allows you to work with your page types in a more compositionable manner. Let’s say we want to define some contact information properties and these will be used on the odd number of page types. We would then declare a class of what is known as a PropertyGroup.

public class ContactInfo : PageTypePropertyGroup
{
    [PageTypeProperty]
    public virtual string Name { get; set; }

    [PageTypeProperty]
    public virtual string PhoneNumber { get; set; }
}

This class contains standard page type property definitions. To use this group of properties you’d simply declare a property of this type and mark it with the special PageTypePropertyGroup attribute.

[PageType]
public class SomePage : TypedPageData
{
    [PageTypePropertyGroup]
    public virtual ContactInfo Contacts { get; set; }
}

[PageType]
public class SomeOtherPage : TypedPageData
{
    [PageTypePropertyGroup]
    public virtual ContactInfo Contacts { get; set; }
}

The name of the properties will be a combination of the property name of the property group, a dash, and then the property name of each the page type properties definied in that property group class. So the page type SomePage above would look like this in admin mode

This is a feature I’ve been hoping for ever since I started using PTB and I’m pretty sure I’m not the only one.