Kentico Cloud View Model - Locally cached & Strongly Typed?

The Kentico Cloud Deliver API uses a CDN internally so that your content is available anywhere in the world in a few milliseconds. Even with that awesome speed, wouldn't it be great to locally cache your content?

Also, what if you prefer working with strongly typed view models?

This is where the EmmTi.KenticoCloudConsumer.EnhancedDeliver NuGet package (still in beta) comes to the rescue.

Strongly typed

NuGet Package

This project is available as a nuget package at https://www.nuget.org/packages/EmmTi.KenticoCloudConsumer.EnhancedDeliver/

To install EmmTi.KenticoCloudConsumer.EnhancedDeliver, run the following command in the Package Manager Console

PM > Install-Package EmmTi.KenticoCloudConsumer.EnhancedDeliver

Quick Setup Guide

Initial Setup

  1. Follow the steps in the Kentico Dancing Goat Example to setup a sample site.
  2. Add the following config setting in appsettings of web.config. Tweak the cache time as you see fit.
    <add key="DeliveryContentCacheTimeSeconds" value="300"/>
    

Creating the View Model

  1. Create a new ViewModel and inherit from BaseContentItemViewModel
  2. Add a ItemCodeName constant with the code name for the Kentico Content Type
  3. Create public properties for other Content Elements
  4. Override MapContentForType method and map each field - See example
using EmmTi.KenticoCloudConsumer.EnhancedDeliver.Helpers;
using KenticoCloud.Deliver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EmmTi.KenticoCloudConsumer.EnhancedDeliver.Sample.Models
{
    public class SampleViewModel : BaseContentItemViewModel
    {
        public const string ItemCodeName = "article";
        public HtmlString BodyCopy { get; set; }
        public string MetaDescription { get; set; }
        public string MetaKeywords { get; set; }
        public Dictionary<string, string> Personas { get; set; }
        public DateTime PostDate { get; set; }
        public List<SampleViewModel> RelatedArticles { get; set; }
        public string Summary { get; set; }
        public Asset TeaserImage { get; set; }
        public string Title { get; set; }

        protected override void MapContentForType(ContentItem content, int currentDepth)
        {
            BodyCopy = new HtmlString(content.GetString("body_copy"));
            MetaDescription = content.GetString("meta_description");
            MetaKeywords = content.GetString("meta_keywords");
            Personas = content.GetTaxonomyItems("personas");
            PostDate = content.GetDateTime("post_date");
            RelatedArticles = content.GetModularContent("related_articles").GetListOfModularContent<SampleViewModel>(currentDepth + 1);
            Summary = content.GetString("summary");
            TeaserImage = content.GetAssets("teaser_image").FirstOrDefault();
            Title = content.GetString("title");
        }
    }
}

The Controller

In your Controller return the new ViewModel using the DeliveryFactory

The View

Use the strongly typed model in your View

See a full sample site

Clone an enhanced version of the Dancing Goat Sample site for a working example
https://github.com/emmanueltissera/Deliver-Dancing-Goat-Enhanced

It's just got the Homepage View converted to a strongly typed, locally cached version. I will be updating the rest of the views and code as and when I get some time.

Source Code

The source code for EmmTi.KenticoCloudConsumer.EnhancedDeliver is available on GitHub for you to fork and work on.
https://github.com/emmanueltissera/EmmTi.KenticoCloudConsumer.EnhancedDeliver

Contributions, pull requests and comments are welcome.


This article was originally written on Google's Blogger platform and ported to Hashnode on 17 Sep 2022.