Umbraco 9 : Models Builder

In this post, we will look at what has changed in Umbraco Models Builder between Umbraco 8 and Umbraco 9. 

Models Builder is a tool that ships with Umbraco. This tool helps to generate strongly-typed content models for Umbraco. By default, the content cache in Umbraco returns an IPublishedContent but when using Models Builder, the content cache returns strongly-typed models that implement IPublishedContent. This happens natively, without any need for extra mapping. 

Models Builder has been a part of Umbraco since version 7.4. With version 8.5 the API Models and Dll builder modes were no longer a part of the tool that shipped with Umbraco. To use the API Models which was quite popular, you would have to use the full version of the Models Builder. The full version of the tool has not been updated for Umbraco 9. This is probably the biggest change that developers need to be aware of. 

With Umbraco v8, there was the config setting Umbraco.ModelsBuilder.Enable to completely disable Models Builder using an appSetting. This option is no longer available in Umbraco 9. Similarly, the Umbraco.ModelsBuilder.EnableFactory setting is also not available in Umbraco 9.

When using Models Builder, you would normally set a builder mode. The builder mode drives the generation of models. With v8.5 of Umbraco, the builder modes available were - PureLive and AppData. The PureLive mode generates the models in a dynamic, in-memory assembly. The AppData mode generates the models in App_Data/Models folder, either upon content type changes (LiveAppData) or by generating the models from the backoffice (AppData). In Umbraco 9, the modes available are - InMemoryAuto and SourceCodeInMemoryAuto is similar to PureLive in v8. The models are generated and compiled on the fly, in memory, at runtime. SourceCode models can have two different values - SourceCodeAuto and SourceCodeManual. Both the modes generate the models as C# classes with extension generated.cs and they are stored in the folder umbraco/models by default. With SourceCodeAuto, the models gets generated everytime the content type changes. With SourceCodeManual, models have to be generated manually using the Generate Models button in the Models Builder backoffice dashboard.

Finally, with Umbraco 9, we are looking at the appSettings.json file for all the configurations. With Umbraco 8, which was based on .NET Framework this was done using appSettings in web.config. The configuration for Models Builder can be added to the Umbraco:CMS config section in appSettings.json as shown below.

  "Umbraco": {
    "CMS": {
      "ModelsBuilder": {
        "ModelsMode": "SourceCodeAuto",
        "ModelsNamespace": "Umbraco.Demo.Models",
        "ModelsDirectory": "~/Models"