The recent Umbraco 7.4 release there is an included Model Builder tool. This may not seem like a big deal but it can be very helpful to developers and I recommend embracing it. In essence what it does it let you generate strong typed models in .Net based off your Document Types and Media Types and their hierarchy. This can be done a few ways including at runtime in memory or with pre-compile DLL’s. Then in Views, Macro’s and Controllers these strong typed models can be used instead of normal code to reduce potential coding errors and typos, and to provide IntelliSense in Visual Studio if you do editing there. These changes for the most part are non-breaking and the old style can still be used.
In short if will replace the first example template with the second.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@Model.Content.GetPropertyValue(“myProperty”)
@(Umbraco.Media(CurrentPage.myImage).umbracoFile)
@Model.Content.GetPropertyValue(“myProperty”, true, “Missing”)
@Umbraco.Field("myProperty ", recursive: true, altFieldAlias: "defaultProperty")
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<DocumentType>
@Model.Content.MyProperty
@(((Image)Model.Content).MyImage.UmbracoFile
@Model.Content.GetPropertyValue(s => s.MyProperty, true, “Missing”)
@Model.Content.GetPropertyValue(s => s.MyProperty, true, Model.Content.DefaultProperty)
This article describes the following approach: Generating C# models in the live Umbraco Instance and then pulling them down into the generated models down into the source Visual Studio Project and pre-compiles them there in the main application DLL. This has some major benefits over other approaches including:
- All code is pre-compiled and no generation or compilation is happening when the website is actually running or starting up.
- Models generation isn’t dependent on IIS Express and you don’t have to do desin work there or start up slow projects to manually create models.
- Models are generated the same place you edit the document types and generated automatically.
- Models are partial class’s that can be extended and can implement custom interfaces as needed.
- There is no need for any special visual studio plugins or deployment rules to make this work. All parts described will work on any Visual Studio Editions.