rob-mason / kontent-generators-net

.NET code generators for Kentico Kontent.

Home Page:https://kontent.ai

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build status NuGet Stack Overflow

Kontent model generator utility for .NET

This utility generates strongly-typed (POCO) models based on content types in a Kontent project. You can choose one of the following:

ℹ If you want to take a look at the next version supporting all features, checkout the vNext branch. ℹ

How to use for Delivery SDK

To fully understand all benefits of this approach, please read the documentation.

.NET Core Tool

The recommended way of obtaining this tool is installing it as a .NET Core Tool. You can install it as a global tool or per project as a local tool.

Global Tool

  • dotnet tool install -g Kentico.Kontent.ModelGenerator

Local Tool

  • dotnet new tool-manifest to initialize the tools manifest (if you haven't done that already)
  • dotnet tool install Kentico.Kontent.ModelGenerator (to install the latest version

Then you can start using the KontentModelGenerator command in the command-line right away.

dotnet tool run KontentModelGenerator --projectid "<projectid>" [--namespace "<custom-namespace>"] [--outputdir "<output-directory>"] [--withtypeprovider <True|False>] [--structuredmodel <True|False>] [--filenamesuffix "<suffix>"]

Standalone app for Windows

Latest release: Download

Since the app is self-contained, it's an ideal option for machines without .NET Core or .NET Core SDK installed.

Usage:

KontentModelGenerator.exe --projectid "<projectid>" [--namespace "<custom-namespace>"] [--outputdir "<output-directory>"] [--withtypeprovider <True|False>] [--structuredmodel <True|False>] [--filenamesuffix "<suffix>"]

Linux, Mac OS and other platforms

  • Clone the repository
  • Navigate to the kontent-generators-net\src\KontentModelGenerator folder
  • Run dotnet build -r <RID> to build the app
  • Run dotnet publish -c release -r <RID> to publish the app
dotnet run --projectid "<projectid>" [--namespace "<custom-namespace>"] [--outputdir "<output-directory>"] [--withtypeprovider <True|False>] [--structuredmodel <True|False>] [--filenamesuffix "<suffix>"]

See the list of all RIDs.

Parameters

Short key Long key Required Default value Description
-p --projectid True null A GUID that can be found in Kentico Kontent -> API keys -> Project ID
-n --namespace False KenticoKontentModels A name of the C# namespace
-o --outputdir False \. An output folder path
-g --generatepartials False true Generates partial classes for customization. Partial classes are the best practice for customization so the recommended value is true.
-t --withtypeprovider False true Indicates whether the CustomTypeProvider class should be generated (see Customizing the strong-type binding logic for more info)
-s --structuredmodel False false Generates IRichTextContent instead of string for rich-text elements. This enables utilizing structured rich-text rendering
-f --filenamesuffix False null Adds a suffix to generated filenames (e.g., News.cs becomes News.Generated.cs)
-b --baseclass False null If provided, a base class type will be created and all generated classes will derive from that base class via partial extender classes

CLI Syntax

Short keys such as -s true are interchangable with the long keys --structuredmodel true. Other possible syntax is -s=true or --structuredmodel=true. Parameter values are case-insensitive, so you can use both -s=true and -s=True. To see all aspects of the syntax, see the MS docs.

Config file

These parameters can also be set via the appSettings.json file located in the same directory as the executable file. Command-line parameters always take precedence.

Advanced configuration (Preview API, Secure API)

There are two ways of configuring advanced Delivery SDK options (such as secure API access, preview API access, and others):

  1. Command-line arguments --DeliveryOptions:UseSecureAccess true (syntax)

  2. appSettings.json - suitable for the standalone app release

Example output

using System;
using System.Collections.Generic;
using Kentico.Kontent.Delivery.Abstractions;

namespace KenticoKontentModels
{
    public partial class CompleteContentType
    {
        public string Text { get; set; }
        public string RichText { get; set; }
        public decimal? Number { get; set; }
        public IEnumerable<MultipleChoiceOption> MultipleChoice { get; set; }
        public DateTime? DateTime { get; set; }
        public IEnumerable<Asset> Asset { get; set; }
        public IEnumerable<object> ModularContent { get; set; }
        public IEnumerable<TaxonomyTerm> Taxonomy { get; set; }
        public string UrlSlug { get; set; }
        public string CustomElement { get; set; }
        public ContentItemSystemAttributes System { get; set; }
    }
}

Customizing models - Handling content element constraints

Currently, the generator is built on top of the Delivery API which doesn't provide information about content element constraints such as "Allowed Content Types" or "Limit number of items". In case you want your models to be more specific, this is the best practice on how to extend them:

Model.Generated.cs

public partial class Home
{
    public IEnumerable<object> LinkedContentItems { get; set; }
}

Model.cs

public partial class Home
{
    // Allowed Content Types == "Article"
    public IEnumerable<Article> Articles => LinkedContentItems.OfType<Article>();
	
    // Allowed Content Types == "Article" && Limit number of items == 1	
    public Article Article => LinkedContentItems.OfType<Article>().FirstOrDefault();
}

How to use for Management SDK

Usage:

KontentModelGenerator.exe --projectid "<projectid>" --contentmanagementapi true [--namespace "<custom-namespace>"] [--outputdir "<output-directory>"] [--filenamesuffix "<suffix>"]

Parameters

Short key Long key Required Default value Description
-p --projectid True null A GUID that can be found in Kontent -> API keys -> Project ID
-c --contentmanagementapi True false Indicates that models should be generated for Management SDK
-n --namespace False KenticoKontentModels A name of the C# namespace
-o --outputdir False \. An output folder path
-f --filenamesuffix False null Adds a suffix to generated filenames (e.g., News.cs becomes News.Generated.cs)
-b --baseclass False null If provided, a base class type will be created and all generated classes will derive from that base class via partial extender classes

These parameters can also be set via the appSettings.json file located in the same directory as the executable file. Command-line parameters always take precedence.

Example output

using System;
using System.Collections.Generic;
using Kentico.Kontent.Management.Models.Assets;
using Kentico.Kontent.Management.Models.Items;
using Newtonsoft.Json;

namespace KenticoKontentModels
{
    public partial class CompleteContentType
    {
        public string Text { get; set; }
        public string RichText { get; set; }
        public decimal? Number { get; set; }
        public IEnumerable<MultipleChoiceOptionIdentifier> MultipleChoice { get; set; }
        public DateTime? DateTime { get; set; }
        public IEnumerable<AssetIdentifier> Asset { get; set; }
        public IEnumerable<ContentItemIdentifier> ModularContent { get; set; }
        public IEnumerable<TaxonomyTermIdentifier> Taxonomy { get; set; }
        public string UrlSlug { get; set; }
	public string CustomElement { get; set; }
    }
}

Feedback & Contributing

Check out the contributing page to see the best places to file issues, start discussions and begin contributing.

Wall of Fame

We would like to express our thanks to the following people who contributed and made the project possible:

Would you like to become a hero too? Pick an issue and send us a pull request!

About

.NET code generators for Kentico Kontent.

https://kontent.ai

License:MIT License


Languages

Language:C# 100.0%