Models Builder is a tool that can generate a complete set of strongly-typed published content models for Umbraco. By default, a slimmed down version of Models Builder is embedded with the main Umbraco distribution.
Models can be used anywhere that content is retrieved from the content cache, i.e. in MVC views, controllers, etc. In other words, when using the Models Builder, the content cache does not return IPublishedContent objects anymore, but strongly typed models, implementing IPublishedContent.
For each content, media and member type in the Umbraco setup, the generator creates a *.generated.cs file, corresponding to the type. For instance, a document type with a textstring property named Title, and a rich text editor named BodyText will look like this:
//------------------------------------------------------------------------------// <auto-generated>// This code was generated by a tool.//// Umbraco.ModelsBuilder.Embedded v9.0.0-beta003+b07f6519e7a1c890b534502982612ce6b3fea293//// Changes to this file will be lost if the code is regenerated.// </auto-generated>//------------------------------------------------------------------------------usingSystem;usingSystem.Linq.Expressions;usingUmbraco.Cms.Core.Models.PublishedContent;usingUmbraco.Cms.Core.PublishedCache;usingUmbraco.Cms.Infrastructure.ModelsBuilder;usingUmbraco.Cms.Core;usingUmbraco.Extensions;namespaceUmbraco.Cms.Web.Common.PublishedModels{/// <summary>NewsItem</summary>[PublishedModel("newsItem")]publicpartialclassNewsItem:PublishedContentModel{ // helpers#pragmawarningdisable0109// new is redundant [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded", "9.0.0-beta003+b07f6519e7a1c890b534502982612ce6b3fea293")]publicnewconststring ModelTypeAlias ="newsItem"; [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded","9.0.0-beta003+b07f6519e7a1c890b534502982612ce6b3fea293")]publicnewconstPublishedItemType ModelItemType =PublishedItemType.Content; [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded","9.0.0-beta003+b07f6519e7a1c890b534502982612ce6b3fea293")]publicnewstaticIPublishedContentTypeGetModelContentType(IPublishedSnapshotAccessor publishedSnapshotAccessor)=>PublishedModelUtility.GetModelContentType(publishedSnapshotAccessor, ModelItemType, ModelTypeAlias); [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded","9.0.0-beta003+b07f6519e7a1c890b534502982612ce6b3fea293")]publicstaticIPublishedPropertyTypeGetModelPropertyType<TValue>(IPublishedSnapshotAccessor publishedSnapshotAccessor,Expression<Func<NewsItem,TValue>> selector)=>PublishedModelUtility.GetModelPropertyType(GetModelContentType(publishedSnapshotAccessor), selector);#pragmawarningrestore0109privateIPublishedValueFallback _publishedValueFallback; // ctorpublicNewsItem(IPublishedContent content,IPublishedValueFallback publishedValueFallback): base(content, publishedValueFallback) { _publishedValueFallback = publishedValueFallback; } // properties ///<summary> /// BodyText ///</summary> [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded","9.0.0-beta003+b07f6519e7a1c890b534502982612ce6b3fea293")] [ImplementPropertyType("bodyText")]publicvirtualglobal::Umbraco.Cms.Core.Strings.IHtmlEncodedString BodyText =>this.Value<global::Umbraco.Cms.Core.Strings.IHtmlEncodedString>(_publishedValueFallback,"bodyText"); ///<summary> /// Title ///</summary> [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded","9.0.0-beta003+b07f6519e7a1c890b534502982612ce6b3fea293")] [ImplementPropertyType("title")]publicvirtualstring Title =>this.Value<string>(_publishedValueFallback,"title"); }}
Now since this is an automatically generated file, it's a bit messy, the important part is that it has all the properties defined and strongly typed:
public virtual global::Umbraco.Cms.Core.Strings.IHtmlEncodedString BodyText => this.Value<global::Umbraco.Cms.Core.Strings.IHtmlEncodedString>(_publishedValueFallback, "bodyText");
And:
public virtual string Title => this.Value<string>(_publishedValueFallback, "title");
Umbraco's content cache returns these objects natively: No need to map, convert or anything; the following code runs:
If your view inherits from UmbracoViewPage<NewsItem> then the model is the content item itself and the syntax is @Model.Title.
Models Builder respects the content types' inheritance tree, i.e. models inherit from each other if required, and mixins (content type compositions) are represented by interfaces.
Models Builder is a "code-after*_" solution. It only generates code from content types that already exist in Umbraco. It is not a "code-first" solution - code-first is a much more complex question.
And once you are using strongly typed models, there are some cool things that you can do.
Installing
The Models Builder is by default embedded in Umbraco. If you need more complex features than what is provided, you still need to add the full package. However, as of right now the package is not updated to be able to handle NetCore or Umbraco V9.
At the core of the strongly typed models "experience" is the IPublishedModelFactory interface. This interface is part of the Umbraco core codebase. It is responsible for mapping the internal IPublishedContent implementations returned by the content cache, to strongly typed models. There is a default factory shipped with Umbraco and it is possible to replace this by custom implementations. When using the default factory, models do not necessarily need to be generated by Models Builder.
Models Builder is one way to generate models for the default, built-in factory. Models can be generated automatically or straight from the Settings section of the Umbraco backoffice, for more info see builder modes.
The following example uses Umbraco.Cms.Core.PublishedCache and IPublishedSnapshotAccessor which are obsolete in Umbraco 15 and will be removed in a future version. For more information, see the Version specific upgrades article.