Skip to content

Working with strongly typed models

Matus Backor edited this page Mar 11, 2019 · 2 revisions


  1. Contents
  2. Strongly-typed models
  3. Defining a model
    1. Typing the properties
      1. Typing simple elements
      2. Typing Linked Items
      3. Typing Rich text
      4. Typing Custom elements
    2. Naming the properties
    3. Examples
  4. Generating models
  5. Retrieving content items
    1. Casting to strong types
  6. Customizing the strong-type binding logic
    1. Adding support for runtime type resolution
      1. Generating custom type providers
    2. Customizing the property matching
    3. Customizing the strong-type resolution mechanism

Strongly-typed models

Besides the basic methods for retrieving content items, the IDeliveryClient interface supports fetching of strongly-typed models.

// Initializes a client
IDeliveryClient deliveryClient = DeliveryClientBuilder

// Basic retrieval

// Strongly-typed model retrieval

This approach is beneficial for its:

  • type safety during compile-time
  • convenience of usage by a developer (@Article.ArticleTitle vs. @Article.GetString("article_title"))
  • support of type-dependent functionalities (such as display templates in MVC)

Defining a model

The models are simple POCO classes, which means they don't have any attached behavior or dependency on an external framework.

Typing the properties

Typing simple elements

Here are the data types you can use for different content type elements:

  • built-in types such as string, decimal and their nullable equivalents for simple elements like Number or Text.
  • IEnumerable<KenticoCloud.Delivery.MultipleChoiceOption> for Multiple choice elements.
  • IEnumerable<KenticoCloud.Delivery.Asset> for Asset elements.
  • IEnumerable<KenticoCloud.Delivery.TaxonomyTerm> for Taxonomy elements.

Typing Linked Items

For linked items elements, use either IEnumerable<T> or any concrete implementation of ICollection<T> such as List<T> or HashSet<T>.

Depending on your scenario, use one of the following as the data type parameter:

  • Specific content type model (e.g., Article) - when the element contains content items based on a single content type.
  • ContentItem - when the element can contain mixed content types and you don't need type safety.
  • object - when the element contains mixed content types and you want the objects to be strongly-typed. See Adding support for runtime type resolution for more details.

Typing Rich text

For Rich text elements, use either string to receive HTML code resolved using string-based resolver as outlined in Rendering linked items in Rich text or IRichTextContent to retrieve rich text content as structured data.

Typing Custom elements

For Custom elements, use the string type. Custom elements behave as simple text-based elements.

Naming the properties

By default, the model properties and content elements are matched by codenames of the elements. The SDK tries to convert the element codenames to PascalCase. For example, a content element with the codename of article_title translates to a property called ArticleTitle.

If you need to change the codename of an element that a property is bound to, you can enrich the property with the Newtonsoft.Json.JsonPropertyAttribute attribute.

public string ArticleTitle { get; set; }


You can find sample models at

Generating models

:information_source: You can save time by generating content type models for your project using the Kentico Cloud .NET code generator utility.

Retrieving content items

Both the GetItemAsync and GetItemsAsync client methods have their corresponding generic overloads, GetItemAsync<T> and GetItemsAsync<T>. The parameters are the same as for the non-generic variants. The only difference is that you need to specify the type parameter <T>.

You can either specify the type directly (e.g., GetItemAsync<Article>) or pass the type as the object class (e.g., GetItemAsync<object>). Use the second approach if you don't know what the type is to let the SDK resolve it during runtime.

This parameter represents the model you want to load. You can specify the parameter in two ways:

  • by using a content type model, for example GetItemAsync<Article>
  • by passing the object class, for example, GetItemAsync<object>

Use the second approach if you don't know what the content type will be and you want the application to resolve content types during runtime.

Casting to strong types

Note that it's possible to cast ContentItem and DeliveryItemResponse to strongly-typed equivalents by calling .CastTo<T>().

Customizing the strong-type binding logic

Adding support for runtime type resolution

The IDeliveryClient instance supports runtime type resolution. This means you can pass the object class instead of explicitly specifying the data type in the model or when calling the GetItemAsync<> method. The data type will be resolved dynamically during runtime.

For example:

object model = await client.GetItemAsync<object>(...);
Type type = model.Item.GetType(); // type will be e.g. 'Article'

For this to work, the SDK needs to know the mappings between the content types and your models.

If you want to use the runtime type resolution in your application, you need to implement the KenticoCloud.Delivery.ITypeProvider interface.

public class CustomTypeProvider : ITypeProvider
  public Type GetType(string contentType)
      case "article":
        return typeof(Article);
      case "office":
        return typeof(Office);


        return null;

Next, you either register the type provider within IServiceCollection

    .AddSingleton<ITypeProvider, CustomTypeProvider>();

or pass it to the DeliveryClientBuilder.

CustomTypeProvider customTypeProvider = new CustomTypeProvider();
IDeliveryClient client = DeliveryClientBuilder

Generating custom type providers

Similarly to models, you can generate implementations of ITypeProvider by using the .NET code generator tool and specifying the --withtypeprovider parameter.

Customizing the property matching

If you want to customize the way content elements and model properties are matched, you need to implement the KenticoCloud.Delivery.IPropertyMapper interface and pass it to the DeliveryClientBuilder class.

// Implements a custom property mapper
public class CustomPropertyMapper : IPropertyMapper
  public bool IsMatch(PropertyInfo modelProperty, string fieldName, string contentType)
    // Add your logic here
    return modelProperty.Name.ToLower() == fieldName;

// Registers the custom property mapper to the IServiceCollection or other framework you are using for dependency injection
    .AddSingleton<IPropertyMapper, CustomPropertyMapper>();

// Registers the custom property mapper within a delivery client using builder
CustomPropertyMapper customPropertyMapper = new CustomPropertyMapper();
IDeliveryClient client = DeliveryClientBuilder

Customizing the strong-type resolution mechanism

If you wish to replace the whole mechanism, you will need to implement the KenticoCloud.Delivery.IModelProvider interface. Compared to the ITypeProvider and IPropertyMapper interfaces, it contains one extra member:

T GetContentItemModel<T>(JToken item, JToken linkedItems);

Your task is to create an instance of <T> by utilizing the data in the item and linkedItems properties.


You can’t perform that action at this time.