Strongly Typed Models News
Posted on February 6, 2016 in umbraco
A few months ago I ended the last post of my Strongly Typed Models serie with:
There are various solutions around. The next post will focus on my own Zbu.ModelsBuilder solution, explain how it works, and why it is not part of Umbraco Core.
The Zbu.ModelsBuilder tool generates C# code for strongly typed models, in an entirely automatic way. One does not need to write a single line of code to get model classes that match the content types that have been defined in Umbraco.
However... in its current state, the CLR (the program that executes the .NET code) does not really support runtime changes to the code it executes. Most developers know, for example, that adding or removing DLLs in the ~/bin directory causes the application to restart.
If the CLR knows about model class MyDocument
, it cannot really replace it with another definition of class MyDocument
with different properties, without restarting. Meaning that anytime a developer would modify a content type, the application would need to restart.
Because this was not really acceptable, we had to disable automatic generation and require that people explicitely generate models—by clicking a big "generate now" button in some dashboard. As a consequence, the "out-of-the-box" experience was not simple enough to be bundled with Core.
PureLive models reloaded
And then at some (recent) point, Shannon a I had the "Eureka" moment (and no, we were not sharing a bath). After a few more trials and errors, we came up with a way to generate models that can be used in views, transparently, without restarting the application: PureLive models.
When PureLive models are activated, model classes are (re-)generated automatically anytime a content type changes, and are immediately reloaded by Umbraco and available in views, meaning that:
@inherit UmbracoTemplatePage
<div>@Model.Content.GetPropertyValue<string>("myProperty")</div>
can become
@inherit UmbracoTemplatePage<MyDocument>
<div>@Model.Content.MyProperty</div>
and expressions such as the following one become possible:
Model.Content.Children<Product>().Where(x => x.Price > 1000);
The caveat being that these classes are available in views, and only in views (ie, not in any other custom code, controllers, etc.). Nevertheless, that was the last missing "simplicity" bit. And so...
Umbraco.ModelsBuilder
Umbraco 7.4 will ship with ModelsBuilder version 3, which on that occasion is renamed from Zbu.ModelsBuilder to Umbraco.ModelsBuilder and undergoes a few changes:
The two previously required NuGet packages have merged into one Umbraco.ModelsBuilder
package that ships with Core, and provides everything that is needed to work with models.
Models builder PureLive mode is enabled by default on Umbraco 7.4 installs. On the other hand, models builder is not enabled during upgrades. In addition, models builder now has a giant "kill switch" that can be used to turn it off entirely.
Due to the name change, basically everything that mentionned "Zbu" has a new name. This includes the namespaces, etc. If you are already using the models builder and have created your own partial files, you may have to update them.
For security reasons, the core Umbraco.ModelsBuilder
does not contain the API server that is used by the Visual Studio extension. A second Umbraco.ModelsBuilder.Api
NuGet package contains that server, and needs to be installed on the website. Also for security reasons, the API server will work only on website compiled in "debug" mode (ie, development sites).
The configuration options have changed slightly. The documentation if currently being updated.
Other than that, the core of the models builder has not changed. If you are using the models builder today, upgrading should require a few adjustments here and there, but nothing more.
What's next?
Updating the documentation, and explaining the different modes and ways to use the models builder in more details. And then, work has already begun on the code generator itself, to add more flexibility, better support for content types composition, and generally answer to the various feature requests that have been expressed.
Interesting times ahead.
There used to be Disqus-powered comments here. They got very little engagement, and I am not a big fan of Disqus. So, comments are gone. If you want to discuss this article, your best bet is to ping me on Mastodon.