Rob West

What's new in the Kentico Kontent Delivery .NET SDK 13.0.1

Published 1 May 2020

I started using the Kentico Kontent Delivery .NET SDK in March 2020 with version 12.0.3. Since then a new major version has been release, 13.0.1, which includes a host of improvements in architecture and usage, so I thought it was worth providing an overview of what's changed.

The version of the Kentico Kontent Boilerplate for ASP.NET Core MVC that I started using was straightforward to adapt to my needs, but it did contain a fair bit of code that dealt with Kontent infrastructure concerns. This has all been addressed in this latest version and the boilerplate is cleaner and simpler as a result.

Overall it is obvious that the direction of travel for the Kentico Kontent .NET developers is towards the ASP.NET Core standards and conventions which is a good thing.

Caching Moved to a Separate NuGet Package

The most obvious change in approach is the creation of a new NuGet package for caching: Kentico.Kontent.Delivery.Caching. There is no longer any need for caching code to live in your solution which provides a better separation of concerns. Caching is a crucial feature for any solution consuming this SDK so it absolutely should be baked in. Overall, the documentation and boilerplate now favour a more standard service collection based instantiation of the delivery client using the ASP.NET Core dependency management framework. This makes for a much cleaner instantiation with less ceremony and simpler syntax. We have gone from this:

IDeliveryClient BuildBaseClient(IServiceProvider sp) => DeliveryClientBuilder
    .WithOptions(_ => deliveryOptions)
    .WithTypeProvider(new CustomTypeProvider())
    .WithContentLinkUrlResolver(new CustomContentLinkUrlResolver())
    .Build();
services.AddCachingClient(BuildBaseClient, options =>
{
    options.StaleContentExpiration = TimeSpan.FromSeconds(2);
    options.DefaultExpiration = TimeSpan.FromMinutes(10);
});

to this:

services.AddDeliveryClient(Configuration);
services.AddDeliveryClientCache(new DeliveryCacheOptions());

Nice! The default values for the DeliveryCacheOptions are 10 minutes for expiration and 10 seconds for stale content which are fine by me and save typing.

New ASP.Net Core Package

The previous iteration of the boilerplate code used a HtmlHelper extension to render assets as responsive images. There was nothing wrong with this as such, but it did require a static property to be set in the startup configuration code as a static extension cannot have injected dependencies which felt a little bit messy. It also missed an opportunity to use one of the cool features of ASP.NET Core in tag helpers. This has now been addressed by the creation of a new Kentico.Kontent.AspNetCore NuGet package. This makes rendering responsive images so much simpler. We have gone from this:

@Html.AssetImage(@Model.TeaserImage.First(), title: @Model.TeaserImage.First().Name, cssClass: "img-responsive", sizes: new ResponsiveImageSizes(300)
    .WithMediaCondition(new MediaCondition { MinWidth = 769, ImageWidth = 300 })
    .WithMediaCondition(new MediaCondition { MinWidth = 330, MaxWidth = 768, ImageWidth = 689 }))

to this:

<img-asset asset="@Model.TeaserImage.First()" class="img-responsive" default-width="300">
    <media-condition min-width="769" image-width="300" />
    <media-condition min-width="330" max-width="768" image-width="689" />
</img-asset>

Which makes the view so much simpler to understand and construct. An ImageTransformationOptions class is introduced to specify the responsive image widths required, which is configured in the Startup:

services.Configure<ImageTransformationOptions>(Configuration.GetSection(nameof(ImageTransformationOptions)));

and can then be injected into the tag helper using the options pattern:

public AssetTagHelper(IOptions<ImageTransformationOptions> imageTransformationOption = null)

which is a much better approach. We have the added advantage that a bunch of classes can be removed from the boilerplate solution.

This NuGet package is a version 0.8 at the time of writing, so obviously early days. The only other piece of functionality in it at the moment is the webhook signature verification middleware to handle authentication of webhook requests. Again, this allows some code to be removed from the boilerplate and creates a better separation of concerns.

Namespace Breaking Changes

The interfaces for the SDK have been shifted into the Kentico.Kontent.Delivery.Abstractions namespace. This will lead to missing namespace references throughout your projects as interfaces and classes shift from Kentico.Kontent.Delivery. I guess this is the major reason for the major version bump.

Coming Next

The latest prerelease is 14.0.0 alpha 2. The main update I can see on this version is a commit which unifies assets and inline images. Both now implement the IImage interface which means they both use Url and Description instead of Src and AlText. This will lead to a more unified ability to render these items which is a good design change.

© 2023 Rob West. All Rights Reserved. Built using Kontent and Gatsby.