What is Fortis & why should you use it?

Fortis is a framework that allows you to encapsulate parts of the Sitecore API and model your Sitecore templates and fields using interfaces. It’s primary purpose is to help speed up development and improve the quality of the work being developed. The framework is very lightweight primarily wrapping the Sitecore API and simplifying regularly written tasks.

Key benefits in a nutshell

  • “Magic” strings used for accessing fields are encapsulated in a central place providing compile time, instead of run time, checking.
  • Use the built in power of .NET C# to check whether an item inherits from a specific template and filter lists of items.
  • Interfaces help you unit test and abstract away your business logic.
  • Modelled Sitecore templates give your developers knowledge of what fields are contained on an item.
  • Quickly and safely access the correctly typed value of a field.

Below we’ll outline, in a bit more detail, a few key areas where using Fortis will help you and your developers build more robust solutions and far quicker.

Modelling Sitecore templates

One of Sitecore’s features is the fact you can have templates inheriting from more than one  template. This poses a problem in .NET C# as it does not support multi-inheritance of classes, to get around this Fortis utilises interfaces to ensure templates have the correct fields. By using interfaces we also gain the benefits of them in that we can use the built in functionality of .NET C# to check if templates inherit other templates and then cast between them. A lot of the Sitecore API utilises “god” and static classes along with the need, in many cases, of a context which makes unit testing your business logic much more difficult. As Fortis uses interfaces and sits between the Sitecore API and your code it will allow you to unit test your code much more easily.

Define the PageTemplate as an interface and let it implement several other templates in Sitecore.

public interface IPageTemplate : IItemWrapper, ISEOTemplate, INavigationTemplate, IContentTemplate
{
}

You can use “is” to check if an item is of a certain template.

if (myItem is ISEOTemplate)
{
    // Do something
}

You can also cast between the interfaces, further to this because Fortis use interfaces you can easily build abstract methods that don’t have a dependency on Sitecore.

var contextItem = itemFactory.GetContextItem<IPageTemplate>();
var seoContent = (ISeoTemplate)contextItem;

Accessing fields

Traditional Sitecore

var fieldValue = myItem["My Field"];

Fortis

var fieldValue = myItem.MyField.Value;

By encapsulating the item object and its fields we’re able to gain some immediate benefits.

  • Know what fields are available on the item and eliminate magic strings.
  • Compile time checks where fields are being accessed, if a field name on a modelled template changes the solution won’t build. With traditional Sitecore you won’t know you’re accessing a field that doesn’t exist until run time.

Accessing fields of different types

Not only can a developer see all the fields available on a template they are also able to immediately see what type of field it is and easily access a correctly typed value.

Traditional Sitecore

try
{
	CheckboxField myCheckboxField = (CheckboxField)myItem["My Checkbox Field"];

	bool booleanValue = myCheckboxField.Checked;

	DateField myDateTimeField = (DateField)myItem["My Date Time Field"];

	DateTime dateTimeValue = myDateTimeField.DateTime;
}
catch (Exception ex)
{
	// One of the fields probably wasn't the correct cast
}

Fortis

bool booleanValue = myField.MyCheckboxField.Value;
DateTime dateTimeValue = myField.MyDateTimeField.Value;

In the traditional way of using the Sitecore API we don’t actually know what kind of field type we’re dealing with. Fortis simplifies this by mapping the different field types to strongly typed interfaces each of which have a corresponding Value property.

Fortis has much more functionality than what has been outlined here, we suggest heading over to the Quick Start section to see how Fortis handle common situations when developing a Sitecore site.