Kentico Cloud View Model - Locally cached & Strongly Typed?

Technical Director at Luminary. Umbraco 3x MVP.
Search for a command to run...

Technical Director at Luminary. Umbraco 3x MVP.
No comments yet. Be the first to comment.
Instructions

How does this even compute? What has digging a trench have to do with Software Engineering and building products?

My workplace Luminary turned 24 on 1-July-2023.

Headless in Umbraco core

Automate meeting host rotations within your team with some smarts

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.

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
web.config. Tweak the cache time as you see fit. <add key="DeliveryContentCacheTimeSeconds" value="300"/>
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");
}
}
}
In your Controller return the new ViewModel using the DeliveryFactory
Use the strongly typed model in your View
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.
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.