Strong Types Explained - Usage[email protected] Czech RepublicMember, Administrator, Kentico Staff admin
edited April 2017 in Back-end Development

In this short series of articles, we'll explain one of the greatest key enhancements in our Delivery .NET SDK: the strong types.


Getting Strongly Typed Content Items

While you can still use the traditional approach, you now can get items off of the DeliveryClient object in a strongly typed format.

Traditionally, you called:

var response = await deliveryClient.GetItemAsync("on_roasts");

This returned a ContentItem object with just two properties:

  • dynamic Elements
  • ContentItemSystemAttributes System

Whereas now, you can call the generic overload of the GetItemAsync method:

var response = await deliveryClient.GetItemAsync<Article>("on_roasts");

This will return an Article object with the following members:

  • IEnumerable<TaxonomyTerm> Personas
  • string Title
  • IEnumerable<Asset> TeaserImage
  • DateTime? PostDate
  • string Summary
  • string BodyCopy
  • IEnumerable<object> RelatedArticles
  • string MetaKeywords
  • string MetaDescription
  • string UrlPattern
  • ContentItemSystemAttributes System

That's a lot better, isn't it?

With the traditional approach, you had to explicitly cast each of the properties (content elements) to a proper type through proprietary helper method calls:

var bodyCopy = response.Item.GetString("summary");
var postDate = response.Item.GetDate("post_date");

Now, everything is properly typed.

What's more, even the RelatedArticles property, in fact, points to an IEnumerable<Article> collection, in this case. The items in that collection are represented by heap objects with proper types. We'll get to that point in part 5 of this series.

The DataAnnotations Attributes

Sometimes, not all data can be distinguished with .NET types. For example, in Kentico Cloud, an e-mail address is nothing but a string:

[email protected]

The same applies to phone numbers if they contain the plus sign in the beginning and dashes in the middle:


How can you make sure that the two properties of the same type string have different semantics? By using System.ComponentModel.DataAnnotations attributes.

Let's take a few examples in our MVC sample site:

public string Phone { get; set; }

public string Email { get; set; }

public string LongDescription { get; set; }

By decorating the properties in the model class with a few attributes you give a signal about the fine-grained type. The framework, MVC, WinForms and various applications, have been taking these attributes into account over time. Using these attributes became a convention among parts of the .NET world.

But how do you decorate properties of a generated class? If you re-generate classes, the attributes will get lost. The obvious solution - creating 'mirrored' properties in the same class - can't be done.

Fortunately, the MetadataTypeAttribute comes to the rescue.

Just create another simple class with properties with the same name, of the same type and visibility. Decorate these with DataAnnotations attributes and decorate the original class with the MetadataType attribute. Like that:

using System.ComponentModel.DataAnnotations;

namespace DancingGoat.Models
    public partial class Coffee

    public class CoffeeMetadata
        public string ShortDescription { get; set; }

        public string LongDescription { get; set; }

Wrapping Up

In this part of the series, you've learned how quickly you can adjust your app to get strongly typed content items. You also know how to save yourself some coding with DataAnnotations attributes.

What about you? Do you like the way the strong types are implemented? How would you like to use it in your .NET projects? Is there anything you miss in the article or in the implementation? For example, would you like the code generator to also generate the metadata files? Let us know in the comments below or do a pull request into the generator repo directly!

Now, let's continue to the part 3 to show how you can make quick provisions to support inheritance among generated types.

Sign In or Register to comment.