Umbraco 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.

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.

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.

Leave a comment

*
*