DEV Community

loading...
Cover image for Kentico Xperience Design Patterns: MVC is Dead, Long Live PTVC

Kentico Xperience Design Patterns: MVC is Dead, Long Live PTVC

seangwright profile image Sean G. Wright ใƒป12 min read

I've written previously about the best ways to use Kentico Xperience's Page Builder. We should be using the powerful Page Builder as the place in an Xperience site where we combine raw structured content with design.

Now, I'd like to introduce a pattern I'm calling Page Templates + View Components (PTVC), explain how it differs from the standard MVC pattern for Kentico Xperience, and detail the benefits it brings.

๐Ÿ“š What Will We Learn?

๐Ÿงฑ Keeping Content Separate From Design

I'm a big fan of keeping content separated from design ๐Ÿ‘๐Ÿฝ. In fact, I presented an entire talk about this topic at Kentico Xperience Connection 2020, called Content Driven Development: Or, Why the WYSIWYG Can Be a Trap.

Content that is design agnostic is more flexible and reusable. A simple example of this principle can be seen by looking at two example Page Type fields:

  • Title - just text, no HTML
  • TitleHTML - text and HTML (maybe even some inline styles)

Now, imagine we want to display these fields in many different places on a site - navigation, call-to-actions, page headings, image alt text, inside the <title> element in the <head>...

Which one is going to be easier to correctly use? The Title field, with no HTML, focusing only on content and no design, is always going to be easier to use ๐Ÿ™‚.

Of course, we're always going to need fields like TitleHTML in our sites, because we want to give content managers the ability to format and apply design to content in very specific ways. But we should remember that this comes at the cost ๐Ÿ’ฐ of reusability and flexibility.

๐Ÿ— Building Pages with MVC

If we look at the Kentico Xperience documentation on application routing we can see there are two ways we can integrate it into our site.

The rest of this post assumes we are using Content Tree based routing, but even if we are using custom routing, many of the benefits of the PTVC pattern still apply.

The first option is basic routing, which we can enable by creating a Razor View in a specific location in our project and naming it to match the the Page Type we want it to render. This approach, as we will see ๐Ÿง, is very similar to to the PTVC pattern.

The other option is advanced routing, which requires creating an ASP.NET Controller class and registering it with an [assembly: RegisterPageRoute()] attribute, and passing data to a Razor View with a View Model class. This is the approach I'm assuming most Xperience developers are using because it's the classic MVC pattern.

๐ŸŽจ Customizing Design with MVC Pages

A question we should be asking ourselves is this:

With advanced routing using MVC, where do we configure the design variations of our Pages?

We could make the entire Page's Razor View an <editable-area />:

<!-- ~/Views/Home/Index.cshtml -->

<editable-area />
Enter fullscreen mode Exit fullscreen mode

This would let us follow the recommendation I made earlier and use the Page Builder as the place where we combine structured content and design ๐Ÿ˜€, but that means each Page needs to be built and managed with the Page Builder.

What if there are thousands of Pages of a specific Page Type in a site? The Content Managers aren't going to appreciate having to go to every single page to update a Widget or design change ๐Ÿ˜’.

What if we don't want to let the structure of the Page to be customized but we do want the design customized? The Page Builder doesn't support this, which means for these kinds of Pages, we need some other place to store all of our design related configuration for a Page ๐Ÿค”.

The most common approach I see is putting this configuration in the Page Type fields - but this, unfortunately, is combining structured content and design, even if the design and content are in separate fields ๐Ÿ˜ž.

As an example, should a Page Type 'design' field HeadingColor be used across the whole site? In lists? In the Page's 'details' View? What if there are 5 different places on a site which aren't using the Page Builder and have different design variations for the same content. How many Page Type fields do we add to represent these variations ๐Ÿ˜ต?

My point is, design is usually dependent on where content is presented, not where it comes from, but Page Types/Pages define the structure and source of content, not the display.

๐Ÿ— Building Pages with Page Templates

If we look at Xperience's documentation on Page Templates, we can see that for basic routes (when using Content Tree based routing) there's not much we need to do:

On sites using content tree-based routing: Basic routes handle everything for you. The system automatically initializes the data context of the page, the page builder feature, and displays the page template used by the page. - Xperience Docs

Awesome! So, what does implementing a Page Template for a Page Type look like?

Registration

We start with the [assembly: RegisterPageTemplate()] attribute which tells Xperience about our Page Template:

[assembly: RegisterPageTemplate(
    identifier: "Sandbox.HomePage_Default", 
    name: "Home Page (Default)", 
    propertiesType: typeof(HomePageTemplateProperties), 
    customViewName: "~/Features/Home/Sandbox.HomePage_Default.cshtml")]
Enter fullscreen mode Exit fullscreen mode

Properties

The propertiesType is optional, but to follow the PTVC pattern, we're always going to create one (even if it's empty) ๐Ÿ‘๐Ÿผ, so lets create that now including the HeadingColor design option we mentioned previously:

public class HomePageTemplateProperties
{
    [EditingComponent(
        DropDownComponent.IDENTIFIER,
        DefaultValue = "XperienceOrange",
        ExplanationText = "Sets the color of the Heading text",
        Label = "Heading Color",
        Order = 0)]
    [EditingComponentProperty(
        nameof(DropDownProperties.DataSource), 
        "Light\r\nDark\r\nXperienceOrange")]
    public string HeadingColor { get; set; } = "XperienceOrange";
}
Enter fullscreen mode Exit fullscreen mode

Filter

We'll also have a Page Template Filter that associates a given request to a Page Template, based on the request's Culture, Page Type, and Parent Page (lot's of power and flexibility here, if it's needed).

I find that for most use cases, when implementing PTVC, these classes are pretty simple, to the point where a base class can abstract away most of the logic:

[assembly: RegisterPageTemplate(
    "Sandbox.HomePage_Default",
    "Home Page (Default)",
    typeof(HomePageTemplateProperties),
    "~/Features/Home/Sandbox.HomePage_Default.cshtml")]

public class HomePageTemplateFilter : PageTypePageTemplateFilter
{
    public override string PageTypeClassName =>
        HomePage.CLASS_NAME;
}
Enter fullscreen mode Exit fullscreen mode

You can find the code for PageTypePageTemplateFilter in the XperienceCommunity.PageTemplateUtilities package, which helps simplify Page Template filter creation and registration ๐Ÿค“.

View

Finally, we create our Razor View (which can be placed anywhere since we can specify the path when registering it - I've always preferred a feature folder based project structure):

<!-- ~/Features/Home/Sandbox.HomePage_Default.cshtml -->

@using Sandbox.Features.Home
@using CMS.DocumentEngine.Types.HEN05 

@model ComponentViewModel<HomePageTemplateProperties>

<!-- ... -->
Enter fullscreen mode Exit fullscreen mode

In the above code snippets, there are no custom Model or Controller classes. Instead, we have:

  • Registration assembly attribute (required)
  • Page Template properties (optional)
  • Page Template Filter (option)
  • Razor View (required)

๐ŸŽจ Configure Design in Page Template Properties

Notice how we added the HeadingColor design option to our Page Template properties... ๐Ÿ˜ฎ

This is a great solution for keeping content and design separate and the primary reason for adopting the PTVC pattern ๐Ÿ˜„!

We can have all of our structured content (eg. Home Page) in Page Type fields, and enable design customizations for each Page in the Page Template properties.

These two sets of data come together in the View:

@using Sandbox.Features.Home
@using CMS.DocumentEngine.Types.HEN05 

@model ComponentViewModel<HomePageTemplateProperties>

@{
    if (Model.Page is not HomePage homePage)
    {
        return;
    }

    string headingClass = Model.Properties.HeadingColor switch
    {
        "Light" => "text-white",
        "XperienceOrange" => "text-primary",
        "Dark" or _ => "text-dark",
    };
}

<h1 class="@headingClass">@homePage.Fields.Title</h1>

<!-- ... -->
Enter fullscreen mode Exit fullscreen mode

Content Management

We can extend the Page Template properties as we enable more design customization for the Page and we can set sensible defaults so that content managers aren't required to configure every single Page ๐Ÿ™‚.

If we start out with only a single Page Template, we don't need to select it when creating a new Page, so our content management workflow doesn't need to change for simple scenarios. It's really an added bonus that Page Templates let content managers select from a developer-curated collection of predefined layouts (Razor Views).

This is really powerful and could be great for a product catalog where we have three ways of displaying products - standard, featured, and accessory. Each would use the same content model (ProductPage), but different layouts (Razor Views) and design customizations (Page Template Properties).

In my opinion, the Xperience product team is playing โ™Ÿ 3D chess with this feature ๐Ÿคฏ.

๐Ÿ–ผ Page Templates + View Components

The Page Template-only Code Smell

Looking at our example above, can you spot the code smell ๐Ÿ‘ƒ?

It's not too stinky yet, but if we leave the code as-is, after a couple months of feature enhancements we are going to end up with business logic (or maybe database access) in our View. That will smell real bad ๐Ÿ’ฉ๐Ÿ’ฉ๐Ÿ’ฉ๐Ÿ’ฉ๐Ÿ’ฉ!

If we find ourselves needing to inject services into our Views, as presented in the Xperience documentation, for anything more than some localization, we've stepped right into a big fresh code smell and we should question our every move ๐Ÿพ!

This code smell is why we have Controllers and Models to begin with - they let us prepare data for a View in advance, so that the View is only concerned with presentation behavior.

๐Ÿงน View Component Cleanup

Fortunately, ASP.NET Core provides us with View Components, the perfect tool to complete the PTVC pattern! View Components let us lift our business logic or model preparation back up into C# and out of a View ๐Ÿ’ฏ.

The PTVC pattern will have us create new View Components any time the Page Template View's ComponentViewModel<Properties> doesn't give us what we need. The View Component will be directly tied to the given Page Template because it will accept as parameters both the Model.Page and Model.Properties.

Here's an example for our Home Page Template:

public class HomePageViewComponent : ViewComponent
{
    private readonly IPageRetriever pageRetriever;
    private readonly IPageUrlRetriever urlRetriever;

    public HomePageViewComponent(
        IPageRetriever pageRetriever,
        IPageUrlRetriever urlRetriever)
    {
        this.pageRetriever = pageRetriever;
        this.urlRetriever = urlRetriever;
    }

    public async Task<IViewComponentResult> InvokeAsync(
        HomePage page,
        HomePageTemplateProperties props)
    {
        // Get other data related to the home page
        var relatedPage = await retriever.Retrieve<TreeNode>(...);

        var url = urlRetriever.Retrieve(relatedPage);

        var model = new HomePageViewModel(
            page, props, relatedPage, url);

        return View(
            "~/Features/Home/Components/_HomePage.cshtml", 
            model);
    }
}
Enter fullscreen mode Exit fullscreen mode

The HomePageViewComponent serves the role of the Controller we would have been using with the MVC approach, and our HomePageViewModel, as seen below, serves the same role as the MVC Model (View Components are an implementation of the MVC pattern):

public class HomePageViewModel
{
    public HomePageViewModel(
        HomePage homePage,
        HomePageTemplateProperties props,
        TreeNode relatedPage,
        PageUrl relatedPageURL)
    {
        Title = homePage.Fields.Title;
        TitleCSSClass = Model.Properties.HeadingColor switch
        {
            "Light" => "text-white",
            "XperienceOrange" => "text-primary",
            "Dark" or _ => "text-dark",
        };
        RelatedPageTitle = relatedPage.Fields.Title;
        RelatedPageURL = relatedPageURL.RelativePath;
    }

    public string Title { get; }
    public string RelatedPageTitle { get; }
    public string RelatedPageURL { get; }
    public string TitleCSSClass { get; }
}
Enter fullscreen mode Exit fullscreen mode

Now, our View Component View will look like a traditional MVC View:

@model Sandbox.Features.Home.Components.HomePageViewModel

<h1 class="@Model.TitleCSSClass ">
    @Model.Title
</h1>

<p>Related: 
    <a href="@Model.RelatedPageURL">
        @Model.RelatedPageTitle
    </a>
</p>

<!-- ... -->
Enter fullscreen mode Exit fullscreen mode

And, with that, we've cleaned up our Page Template, separated content and design, and covered all the parts of the PTVC pattern ๐Ÿฅณ๐ŸŽŠ!

๐Ÿ’ฐ Benefits of PTVC over MVC

Let's quickly review the benefits that PTVC brings when compared to the traditional MVC approach:

๐Ÿงฑ PTVC enables separating content from design

The separation of content and design is key to a maintanable Kentico Xperience site with flexbile, reusable content that can be easily validated for quality.

โœ” PTVC lets us maintain this separation while still enabling design customization with its Page Template Properties.

โŒ MVC forces us to maintain design settings in Page Type fields when not using the Page Builder.

๐Ÿ›ฃ PTVC encourages using Basic Content Routing

โœ” Basic Content Routing should be ideal for most (if not all) Pages in our site. Let Xperience handle mapping a URL to a TreeNode instance.

โŒ MVC gives more control over routing at the cost of more complexity. Why add complexity when it's not needed?

๐Ÿ‘ถ PTVC is simple for simple cases

โœ” If your Page relies entirely on the Page Builder, your Page Template View could be empty except for a <editable-area ... />. A single Page Template Filter class can handle multiple Templates.

โŒ MVC always requires a Controller and a Model.

๐Ÿ’ช๐Ÿพ PTVC grows with the requirements

โœ” PTVC isn't restricted to simple use cases. By adopting View Components to gather and transform our content, we can continue to encapsulate data access and presentation logic outside of our Razor Views.

Without child actions in ASP.NET Core, we are encouraged to use View Components anyway, so traditional MVC approaches will have us creating them as well.

โŒ This means with MVC we'll have MVC Controllers and View Models + View Component classes and View Models.

๐Ÿ–ผ PTVC enables future customization

โœ” We can customize View layouts and design by adding more properties to a Page Template Properties class, or adding a new Page Template View for a given Page Type.

For example, one Page Template could have a pre-defined layout with only customizations for text or background colors, while another Page Template (for the same Page Type) could rely entirely on an <editable-area />, giving content managers full control when they need it!

โŒ This kind of flexibility isn't possible with MVC without pushing design content into Page Type fields and building some logic to switch the Controller's View based on a Page Type field - this would need to be repeated for every Page Type that needed this flexibility.

With Page Templates, we get this for free.

๐Ÿ’ต PTVC is easy to cache

โœ” With the release of Kentico Xperience 13 Refresh 1 we can set cache dependencies on ASP.NET Core's <cache> Tag Helper.

By wrapping our View Component in the <cache> Tag Helper and setting the correct cache dependencies, we can effectively cache the entire body of our Page!

โŒ With MVC, the Controller will always execute and we'll always have to create a View Model, even if we wrap parts of the View in the <cache> Tag Helper.

๐Ÿค“ Conclusion

Keeping a clear separation between structured content and design makes our content more reusable and allows to apply design to that content contextually - the right design in the right place at the right time ๐Ÿง .

Building Pages in Kentico Xperience with MVC components lacks a place for us to store design related options for a Page when not using the Page Builder (which is the ideal place to combine structured content and design).

Page Templates with properties, which have the same functionality as Widget or Section properties, give us the ability to associate design customizations at the Page level, aligning with the separation of content and design.

If we combine Page Templates with View Components, we gain the content/design separation benefits and keep our business logic and data access out of the View ๐Ÿ‘๐Ÿพ.

My recommendation is to ditch MVC (unless you need it for custom routing), and build every Page following the PTVC pattern, even when there's only a single Page Template and no design customizations in the properties. Starting with this pattern as a foundation opens up the possibility for future flexibility when it's needed, at little upfront cost.

What are your thoughts on MVC vs PTVC? Have you discovered other ways to keep a separation between content and design, or business logic and presentation?

Let me know in the comments below...

And, as always, thanks for reading ๐Ÿ™!

References


Photo by Jordan Madrid on Unsplash

We've put together a list over on Kentico's GitHub account of developer resources. Go check it out!

If you are looking for additional Kentico content, checkout the Kentico or Xperience tags here on DEV.

Or my Kentico Xperience blog series, like:

Discussion (3)

pic
Editor guide
Collapse
yuriyskentico profile image
Yuriy Sountsov

I may have missed it, is there a sample app that shows a full implementation of PTVC?

Collapse
seangwright profile image
Sean G. Wright Author

There's no link to external code, but there is also nothing special with the code. If you can create a Page Template and a View Component, then you've followed the pattern.

Was there something specific you were looking for an example of?

Collapse
yuriyskentico profile image
Yuriy Sountsov

No, nothing specific, just the whole thing in general.