Strong Types Explained - Code Generator[email protected] Czech RepublicMember, Administrator, Kentico Staff admin
edited May 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.



In one of our first webinars, we showed how easy it was to display Kentico Cloud content in .NET MVC sites using the Delivery .NET SDK.

Now, let me be informal for a moment: We've made the .NET developers' experience way sexier! We've introduced strong types and the automatic typing at runtime.

In .NET, most of the time you want strong types. Why?

  • Your code knows what content item has what elements, of what kind.
  • You can use generic strongly typed collections.
  • Most bugs can be revealed early as you write code or at compile time, not at runtime.
  • You get IntelliSense support.
  • You name it …

Moreover, the sweetest spot lies in ASP.NET MVC. Why?

In this short series of articles, we'll explain how you can use strong types at best.

The Code Generator

Of course, you don't have to generate your strong types manually. We've created a simple but powerful command line generator.

You can use it either manually, in a pre-build event or as part of your complex continuous integration process.

The generator is open source and available on GitHub (as well as the .NET SDK). Just download the .exe file and run:

CloudModelGenerator.exe --projectid "<projectid>" [--namespace "<custom-namespace>"] [--outputdir "<output-directory>"] [--withtypeprovider]

The following is a real-world example of our MVC sample app

CloudModelGenerator.exe --projectid "975bf280-fd91-488c-994c-2f04416e5ee3" --namespace "DancingGoat.Models" --outputdir "C:\Users\jan.lenoch\Source\Repos\cloud-sample-app-net\DancingGoat\Models\ContentTypes" --withtypeprovider

This is what it produces:

The utility connects to the public API endpoint for content types, fetches the names, code names, content elements, their types etc. It's easy; you don't even have to provide any credentials since the endpoint is public.

What Types Does It Produce

The following table shows all Kentico Cloud element types with their corresponding .NET types.

Content Type .NET Type
Text string
Rich text string
Number decimal?
Multiple choice IEnumerable of MultipleChoiceOption
Date & time DateTime?
Asset IEnumerable of Asset
Modular content IEnumerable of object
Taxonomy IEnumerable of TaxonomyTerm
URL slug string

All generated classes are partial. If you extend them in separate files, you can re-generate the original ones without worrying about losing your code.

The Type Provider

The --withtypeprovider option makes the generator also produce a simple class that maps code names of content types to the generated .NET types. Like so:

switch (contentType)
    case "about_us":
        return typeof(AboutUs);
    case "accessory":
        return typeof(Accessory);

The important point is that you can override this simple logic with your own one.

As an example, you can create several Kentico Cloud content types that don't necessarily need separate .NET types. You can name these content types with some prefix (like 'Aggregated') and return the common .NET type for all of them:

if (contentType.StartsWith("Aggregated"))
    return typeof(SomeAggregateType)

You can mix and match to your liking.

Unless you wish to get goodies explained in part 4, you can omit the --withtypeprovider option altogether. This way you can rely on automatic matching (codenames converted to PascalCase).

Wrapping Up

In this part of the series, we've explained why strong types for content items are so important. Literally, all of the goodness of .NET comes to life with strong types. Also, we've covered how quickly you can generate (and re-generate) code for strong types using the code generator utility.

What about you? Do you like the idea of strongly typed content? In which situations do you use strongly typed content? Is there anything you miss in the article, the code generator or in the overall concept of strongly typed content? Let us know in the comments below!

Now, let's continue to the part 2 that covers how easily you can use strong typing.

Sign In or Register to comment.