Umbraco: Is Code First on the Way?

It’s no secret that I’m a fan of uSiteBuilder, especially the code first functionality on document types. Whilst uSiteBuilder is a great add-on, I have always thought that code first deserves its place in Core.

When code first was announced in Contour 3.0 I was hopeful that this bode well for the code first approach popping up in other areas of Umbraco; it turns out that this hope of mine may in fact come to fruition.

Tom's Discovery

A colleague of mine, Tom, was looking into unit testing Umbraco; as version 4.x isn’t particularly unit test friendly he decided to sneak a peek at the source of the upcoming release, Umbraco 6.

Here, in the tantalisingly interesting namespace Umbraco.Tests.CodeFirst, he found this code:

using Umbraco.Tests.CodeFirst.Attributes;
using umbraco.editorControls.textfield;
using umbraco.editorControls.textfieldmultiple;




namespace Umbraco.Tests.CodeFirst.TestModels
{
    [ContentType("home", AllowedChildContentTypes = new[] { typeof(ContentPage) })]
    public class Home : ContentTypeBase
    {
        [PropertyType(typeof(TextFieldDataType))]
        public string SiteName { get; set; }
		
        [Alias("umbSiteDescription", Name = "Site Description")]
	[PropertyType(typeof(textfieldMultipleDataType))]
	public string SiteDescription { get; set; }
    }
}

Similarites and Differences with uSiteBuilder

Like uSiteBuilder this is a class full of properties that are decorated with specific attributes. There are a few subtle differences however that can be used for speculation.

Base Class

Firstly in uSiteBuilder you subclass from DocumentTypeBase, whereas here you inherit from ContentTypeBase. In the Umbraco database schema the cmsContentType table contains data about both document types and media types. Does this mean that we may be able to keep code first implementations of media types in source control too? This would be a really nice feature, especially for sites with large and diverse media libraries.

Data Type Association

There is also a difference in how data types are assigned to a property. In uSiteBuilder the data type is chosen from an enum, in this Umbraco implementation a Type is used.

// uSiteBuilder
[DocumentTypeProperty(UmbracoPropertyType.RichtextEditor)]
public string MainContent { get; set; }
// Umbraco
[PropertyType(typeof(TinyMCEDataType))]
public string BodyContent { get; set; }

Convention over Configuration

My favourite additional feature that Umbraco adds though is a convention over configuration method similar to that made popular by Rails. The Umbraco implementation allows for default data types to be assigned to a property based on its type without the need for attributes. The default data types that I found in this class show that bool, int, DateTime and string can automatically render yes/no, numeric, date picker and textstring data types without specifying them in the attributes.

This means that Plain Old CLR Objects (POCOs) can be used to create simple content types. This example is in the Umbraco tests.

This can make creating content types incredibly quick, and for hack projects like the one I did for AFCB this could make rapid prototyping projects a breeze. Take for example these two implementations of the same simple document type.

// uSiteBuilder
[DocumentTypeProperty(UmbracoPropertyType.Textstring)]
public string MainTitle { get; set; }




[DocumentTypeProperty(UmbracoPropertyType.Numeric)]
public int SearchWeighting { get; set; }




[DocumentTypeProperty(UmbracoPropertyType.DatePicker)]
public DateTime PublishDate { get; set; }




[DocumentTypeProperty(UmbracoPropertyType.TrueFalse)]
public bool IsHiddenFromMenu { get; set; }
// Umbraco
public string MainTitle { get; set; }




public int SearchWeighting { get; set; }




public DateTime PublishDate { get; set; }




public bool IsHiddenFromMenu { get; set; }

A nice touch on top of this is that the alias and name will be inferred from the property names. So the POCO’s MainTitle property will be rendered in the CMS as “Main Title” and its alias will be “mainTilte”. A small detail, but nice nonetheless!

Other Stuff

Further interesting features there seems to be:

Mixin support - reducing the code you need to write even more!

Separate Attributes for Sort Order and Description

“Property groups” rather than “tabs” - insight into Umbraco 7 and its new interface maybe?

The Future

I’m really interested to see how this pans out. How will these code first models fit in with Umbraco’s increasingly strong lean to MVC, will these code first models be the models used in the views?

What does this mean for uSiteBuilder? This project has recently picked up speed; its Trello board shows the progress they’re making. Despite being a fan of uSiteBuilder I have to say that I would move over in a flash to a standard Umbraco alternative.

This functionality may not be coming soon though, it may not even come at all, so for now I will happily keep using uSiteBuilder to make my Umbraco development quick, easy and enjoyable!

Comments

comments powered by Disqus