Model Templates

Any templates you wish to work with Fortis need to be modelled, the following examples show how to model your Sitecore templates.

Each template will need an interface and corresponding class created for it, when requesting an item you always pass in the interface and Fortis will construct the correct concrete class for you. The IItemWrapper interface and corresponding ItemWrapper class contain all the base methods and properties which every Sitecore item will have. Even if you don’t model a specific template you can always get the item out via Fortis by requesting it as IItemWrapper. Ultimately it’s up to you how you model your templates as Fortis doesn’t have any restrictions on how the fields are named or implemented.

Key points

  • Interfaces modelling templates must implement IItemWrapper
  • Classes implementing your interfaces must inherit the class ItemWrapper
  • Each interface and class created needs a TemplateMapping attribute added to it, interfaces need the extra parameter of “InterfaceMap”
  • Use the GetField<T>(“Field Name”) method for returning strongly typed versions of the fields
  • As Fortis doesn’t “populate” your objects and just reads from the Item passed in the constructor you have free reign on naming conventions of your fields

Let’s assume we have the following templates in Sitecore;

  • Page
    • Title – Single Line Text
    • Body – Rich Text
    • Image – Image
  • Home Page : Page
    • Related Pages – Multilist

Page Template

[TemplateMapping("{AF49395C-74BB-4ACF-8E01-F2B5BEECA8FE}", "InterfaceMap")]
public partial interface IPage : IItemWrapper
{
	ITextFieldWrapper Title { get; }
	IRichTextFieldWrapper Body { get; }
	IImageFieldWrapper Image { get; }
}

[TemplateMapping("{AF49395C-74BB-4ACF-8E01-F2B5BEECA8FE}")]
public partial class Page : ItemWrapper, IPage
{
	public Page(Item item, ISpawnProvider spawnProvider)
		: base(item, spawnProvider)
	{ }

	public ITextFieldWrapper Title
	{
		get { return GetField<TextFieldWrapper>("Title"); }
	}

	public IRichTextFieldWrapper Body
	{
		get { return GetField<RichTextFieldWrapper>("Body"); }
	}

	public IImageFieldWrapper Image
	{
		get { return GetField<ImageFieldWrapper>("Image"); }
	}
}

Home Page Template

[TemplateMapping("{02F5002C-325E-4E5A-9C93-A97724ED3400}", "InterfaceMap")]
public partial interface IHomePage : IPage
{
	IListFieldWrapper RelatedPages { get; }
}

[TemplateMapping("{02F5002C-325E-4E5A-9C93-A97724ED3400}")]
public partial class HomePage : ItemWrapper, IHomePage
{
	public HomePage(Item item, ISpawnProvider spawnProvider)
		: base(item, spawnProvider)
	{ }

	public ITextFieldWrapper Title
	{
		get { return GetField<TextFieldWrapper>("Title"); }
	}

	public IRichTextFieldWrapper Body
	{
		get { return GetField<RichTextFieldWrapper>("Body"); }
	}

	public IImageFieldWrapper Image
	{
		get { return GetField<ImageFieldWrapper>("Image"); }
	}

	public IListFieldWrapper RelatedPages
	{
		get { return GetField<ListFieldWrapper>("Related Pages"); }
	}
}

Code Generation

In most cases it’s not practical to manually model every template as most Sitecore implementations have a large number of templates especially when inheritance is taken into account. For this reason we highly recommend you use code generation to create all of your modelled interfaces and classes.

There are several approaches you can taken:

While the first two options are certainly viable we recommend using Team Development for Sitecore (TDS) which allows you to write T4 templates and use them against the serialised files it stores in your solution. The Fortis package comes with a header and item T4 template for you to use with TDS although we recommend tailoring it to suit your needs. TDS doesn’t just allow you to easily generate code it will also help your development in general, we highly recommend looking into it!

Next: Select Items