Umbraco 8 Modelsbuilder Extend Partial Class.

Since Umbraco released the models' builder, they have speeded up the building of websites and as the code generated creates the document type and properties for us, this allows us to have intellisense in Visual Studio allowing for strongly types models.

Since the models' builder has all it classes auto-generated in visual studio, this means that we cannot alter the properties within the class as they will be overridden on the next update.

Partials class code.

The code shown is an example of what is generated by the model's builder taken from a demo website I created for this blog

//------------------------------------------------------------------------------
// 
//   This code was generated by a tool.
//
//    Umbraco.ModelsBuilder v8.0.4
//
//   Changes to this file will be lost if the code is regenerated.
// 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web;
using Umbraco.ModelsBuilder;
using Umbraco.ModelsBuilder.Umbraco;

namespace Web.ContentHelper.UmbracoModelBuilder
{
	/// Image And Text
	[PublishedModel("imageAndText")]
	public partial class ImageAndText : PublishedElementModel
	{
		// helpers
#pragma warning disable 0109 // new is redundant
		[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.0.4")]
		public new const string ModelTypeAlias = "imageAndText";
		[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.0.4")]
		public new const PublishedItemType ModelItemType = PublishedItemType.Content;
		[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.0.4")]
		public new static PublishedContentType GetModelContentType()
			=> PublishedModelUtility.GetModelContentType(ModelItemType, ModelTypeAlias);
		[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.0.4")]
		public static PublishedPropertyType GetModelPropertyType(Expression<Func<ImageAndText, TValue>> selector)
			=> PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector);
#pragma warning restore 0109

		// ctor
		public ImageAndText(IPublishedElement content)
			: base(content)
		{ }

		// properties

		///
		/// Image Width
		///
		[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.0.4")]
		[ImplementPropertyType("imageWidth")]
		public string ImageWidth => this.Value("imageWidth");

	}
}

As you can see in the example code above, it warns you about modify the code.

Changes to this file will be lost if the code is regenerated.

So how do we add our code to the partial class?

Fortunately, the class is partial; this means that we can create our class within the same namespace and also make that clas partial.

Therefore say we do not add width to the image in the CMS, we can now create a property called ImageDefaultWidth and add a default value to the image width. Now, all we need to do is instead of displaying imageWidth; we can show the value of ImageDefaultValue.

Example code

namespace Web.ContentHelper.UmbracoModelBuilder
{
    public partial class ImageAndText
    {
        public int ImageDefaultWidth => this.Value("imageWidth", defaultValue: 320);
    }
}

Summary

Although this was a simple example, I hope it shows you how easy it is to extend partial classes when working with visual studio and the Umbraco models builder.