The design principle “favor composition over inheritance” from the Gang of Four book “Design Patterns” states that a combination of functionality should be made in a flat structure, rather than in a hierarchical structure. This way, the functionality becomes more reusable, and the combination more flexible.
The principle is originally meant for software architecture but it also applies to data architecture. In this article we will show how the principle could be applied to the main data architecture of Sitecore: templates.
Templates in Sitecore have the following features:
- Templates can inherit from other templates
- Templates can inherit from more than one generation of other templates
- One template can inherit directly from more than one other templates
Although Sitecore enables inheriting from more than one generation deep, it is generally a better idea to keep the inheritance flat, but broad. This has a few advantages:
- Templates are more reusable. We can inherit from a certain template, to make use of its functionality, without being required to inherit from functionality that is defined deeper in the inheritance chain.
- It is more clear. We are able to see a flat list of the functionality a template incorporates, with having to dig into the (possibly) complex tree of inheritance.
Inheritance with Sitecore templates
When working with inheritance in Sitecore, we have to be aware of the following behaviors:
- All inherited data fields will be available at the combination template
- Field sections with the same Item name will be merged
- Templates can only inherit the Presentation components of one other template
In our example we have a combination template, 3 data templates (without presentation components), and 2 templates with presentation components. In the “Combination Template”, we inherit directly from all other templates. All fields from the data template are directly available in the standard values of the combination template: Field 1, Field 02, and Field 3.
The templates “Data 1” and “Data 2” both contain a field section with the name “Field Section”. The fields of these sections are merged in the combination template. The fields appear in alphabetical order, regardless of the order in which they were added to the Inheritance List of the combination template. That is why “Field 02” comes before “Field 1”, although it comes after that in the Inheritance List. The order of appearance of field section is also alphabetical. (Unless of course the standard field “SortOrder” is set)
It is also a good idea to separate data from presention in your template structure. That is why we have added separate templates, that contain no fields, but that contain presentation components on their standard values. The combination template can only inherit the presentation components (sublayouts and layout) of one other template. The first template in the Inheritance List will be used by Sitecore to determine the presentation components of the combination templates. In our example this will be “Layout 1”. The selected Layout (and referenced .aspx file) on the standard values of that template will appear on the standard values of the combination template, as well as all of its sublayouts and XSLT files. No sublayouts and XSLT files of other templates with presentation will appear.
Note that this applies to the first template that includes presentation components. We could include all the data templates before the “Layout 1” template, and still its presentation components will be inherited. This even applies when the data templates have standard values, as long as the standard values don’t include presentation components.