-
Notifications
You must be signed in to change notification settings - Fork 49
Zbu.ModelsBuilder
Check the releases page for details about the latest release.
Zbu.ModelsBuilder is an experimental tool that can generate a complete set of strongly-typed published content models for Umbraco 7.1.4+. These models can be used anywhere content is retrieved from the content cache, ie in MVC views, controllers, etc. In other words, the content cache does not just return IPublishedContent
objects anymore, but strongly typed models.
For each content (media, and member) type in the Umbraco setup, the generator creates a *.generated.cs file, corresponding the content type, and looking like:
namespace MyModels
{
public partial class NewsItem : PublishedContentModel
{
public string Title { get { return this.GetPropertyValue<string>("title"); } }
public IHtmlString BodyText { get { return this.GetPropertyValue<IHtmlString>("bodyText"); } }
}
}
Umbraco's content cache returns these objects natively: no need to map, convert, anything, the following code just runs:
@inherits UmbracoViewPage<NewsItem>
@using MyModels
<h1>@Model.Title</h1>
@Model.BodyText
The models builder respects the content types inheritance tree, ie models inherit from each other if required, and mixins (content types compositions) are represented by interfaces.
The models builder is a "code-after" solution. It only generates code from content types that already exist in Umbraco. It is not a "code-first" solution -- code-first is a much more complex question.
And once you are using strongly typed models, there are some cool things that you can do!
You can get the nuget package here: https://www.nuget.org/packages/Zbu.ModelsBuilder.AspNet/
The umbraco package can be found here:http://our.umbraco.org/projects/developer-tools/zbumodelsbuilder
And the visual studio plugin can be found here: https://visualstudiogallery.msdn.microsoft.com/3ac3afd4-09db-4c4f-89aa-6ce9e8e7c046
At the core of the strongly typed models "experience" is the IPublishedContentModelFactory
interface, which has been made public with Umbraco version 7.1.4. This interface is part of Core. It is responsible for mapping the internal IPublishedContent
implementations that would be returned by the content cache, to strongly typed models. Although there is a default factory shipped with Umbraco, it is possible to replace it by custom implementations. And even using the default factory, models do not necessarily need to be generated by Zbu.ModelsBuilder. See the IPublishedContentModelFactory page.
Zbu.ModelsBuilder is just one way to generate models for the default, built-in factory. Models can be generated either straight from the Umbraco back-end, or via a console application, or via a Visual Studio extension. See the Using the Models Builder page.