Blog about tips & tricks for CMS enhancement

eric.petersson

Simple tips for structuring Umbraco properties


When building your web application in pure Umbraco Front End MVC with IPublishedContent as your model, things tend to become quite messy with too many hardcoded string properties. Make sure you refer to these in a static class to maintain a good project coverage of your mappings from the Umbraco backoffice model to your properties:

namespace My.Webapplication
{
    public static class Global
    {
        public static class UmbracoProperty
        {
            public const string title = nameof(title);
            public const string mainDescription = nameof(mainDescription);
        }
    }
}

And in your razor view:

@using My.Webapplication;
@model IPublishedContent

<p>@Model.Content.GetPropertyValue<string>(Global.UmbracoProperty.mainDescription)</p>

Instead of:

<p>@Model.Content.GetPropertyValue<string>("mainDescription")</p>

This way you may end up changing your property naming convention at one place instead of several places and your IDE may find references to usage of these constanst way better.