Maximize reuse using polymorphism

When we implement a sitecore project we always have in mind that the site will grow. It will not only get more content, but new sites could be added, and these sites will need some basic functionality that will always be the same. And we do not want to repeat ourselves. Maximum reuse or if you prefer following the DRY principle (Do Not Repeat Yourself) should be most important for the developers. It could be challenging when starting development of a new site, but it really saves a lot of time, not only when adding new site but when changing the common behavior as well.

Share templates between sites

For this purpose, we create shared templates in separate folder. Each site has their base templates and these templates inherit from the shared base template. We call it _Global Base Template.

It would be useless without proper mapping to c# objects. It would be possible to share the common renderings as well or you can event use the workflow inheritance as show in another blog post.

Base services for common tasks

When we manage to map correctly all base templates to inherit from the shared template we then can take the full advantage of it. We can create base services for breadcrumbs, navigation, sitemaps, robots.txt and so on. All the base service methods are virtual so when a site has custom behavior it can be overriden.

No need to implement common functions

We can focus only on the specific site configuration and we do not have to implement common functions or we can just slightly adjust the behavior without any pain.

_Global Base Page

Shared base template

Page templates on all sites inherit from the site-specific base page templates. All site-specific site templates inherit from shared base templates. We call it _Global Base Page. It contains all common field used on all pages. The site-specific base page templates then extends the global template by site specific fields, like hiding top menu checkbox, that is not relevant for all pages on all sites.

_Global Base Template

_Global Base Template

Mapping to POCO classes

The crucial step is to correctly map the shared and site-specific base templates. You get pure c# classes using polymorphism, so you can cast every page to IGlobalBasePage. Then nothing can stop you from writing shared services that use the shared base page.

GlobalBasePage

GlobalBasePage

BasePage

BasePage

ArticlePage

ArticlePage