Creating classes from your content model
![20180627-Classes-from-your-content-model-update](https://images.ctfassets.net/fo9twyrwpveg/5TFXhevTkkIQi06s20gm4g/81790f0d8e0423db1027dfaeb71539d3/27-June-Classes-from-your-content-model-update.png?fm=jpg&q=90&w=1536)
In my , I showed how to use the Contentful.CodeFirst package to let your C# classes act as the basis of your content model in Contentful. But what happens if your content model already exists and you want to create corresponding, strongly-typed C# models? That's a lot of boilerplate code to write—today I'm happy to release a new tool that helps you do just that.
With the release of .NET Core 2.1, "global tools" is a new feature that will become generally available. This means that you can run dotnet tool install -g your-tool-id
and install any tool directly from NuGet for it to become available to call from your command line.
To install the model creator tool, make sure you have the latest .NET Core SDK version installed and run dotnet tool install -g contentful.modelscreator.cli
from your command line.
You should now be able to run contentful.modelscreator.cli --help
to list all the available commands. Only the access token and space ID are mandatory, but there are a few other commands that are interesting:
The
--namespace
or-n
option allows you to specify the namespace in which the classes should be created.The
--path
or-p
option allows you to specify where the classes should be created on your hard drive.
If you test it with credentials from our Example App, you get the following result:
What’s happened is that the classes have been created in my current directory where, for example, the Course class looks like this:
It automatically resolves the field types to the correct type and sets the name of the property to correspond with the id of the field in Contentful. It also tries to resolve reference field to the correct generic type, List<Lesson>
and List<Category>
for example, by inspecting the content type validations of the field. If there's a single content type validation, the class that content type corresponds to will be used as the generic type of that property.
As seen in the class above, the namespace will be set to "Replace.Me.NameSpace" if it's not provided in the call. This can then be easily searched and replaced.
To set the namespace in the initial call, just alter it like so...
... and the namespace declaration will look like this:
That's the same way you can change the default path, which is the current directory, by using the -p parameter.
As a result, the classes will now be created at c:\temp.
If the path already contains the files, you will be prompted to overwrite (if you've made changes you want reflected in an existing class) or skip (if there are no changes) that file.
If you want to automatically overwrite files in the directory, you can use the --force
or -f
command and the files will be overwritten without any prompts.
Leaving the command line
Not everyone is crazy about using the command line and would rather have something in the IDE or editor of their choice. The CLI tool above is available as a plugin to both Visual Studio and Visual Studio Code.
To install it in Visual Studio, click Tools --> Extensions --> Updates, search for Contentful and install the Contentful.ModelsCreator plugin.
After successfully installing the plugin, a new number of settings can be found under Tools --> Options.
Once the access token and space id have been set, I can now right click any folder in my solution and the models will be created there.
![models-creator-vs](https://images.ctfassets.net/fo9twyrwpveg/1owVkTy7hC0gyseWiecmaI/87b9728394a5d53acee1b74d458cefba/models-creator-vs.gif)
Similarly, you can install the plugin through the Marketplace in Visual Studio Code by clicking the Extensions tab and searching for Contentful. Once the extension is installed, there are a similar number of settings where you can specify the API key, space id and namespace.
After that, right click on any folder in Visual Studio Code and choose the Create Contentful Models option.
![models-creator](https://images.ctfassets.net/fo9twyrwpveg/1RhCsm84wogO6WMgiMAAak/dddbdee73fe3f529c332d5da78a53891/models-creator.gif)
Discover more and use Contentful
In this article, we looked at a new CLI tool and plugins available to help you create strongly-typed C# classes directly from your Contentful content model. If you want to learn more, check out the CLI and the Visual Studio Code plugin on our Github repository. You can also read more about .NET Core 2.1
If you're new to Contentful, you can easily create an account for free to try what we've run through in this tutorial. Getting a space for your content to get your static site up and running is a breeze; try it for yourself!
Happy coding!