Skip to content
Stephan edited this page Oct 30, 2013 · 24 revisions

ZpqrtBnk Umbraco Models Builder
Copyright (C) Pilotine - ZpqrtBnk 2013
Distributed under the MIT license

About

Intro

An experimental tool that can generate a complete set of strongly-typed published content models for Umbraco 6.2+ to be used in the MVC views. Either from the Umbraco UI, from the command line, or from Visual Studio.

DISCLAIMER: THIS CODE IS UNSTABLE AND FOR INFO ONLY !!

It runs on a modified 6.2.0 Umbraco version so it will not compile on the stock code from Umbraco's repo.

What it does

For each content (and media) type in your Umbraco setup, it creates a *.generated.cs file, corresponding the content type, 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"); } }
  }
}

Which gives you strongly typed models that you can use in MVC views, because they are natively returned by the Umbraco content cache:

@inherits UmbracoViewPage<NewsItem>
@using MyModels
<h1>@Model.Title</h1>
@Model.BodyText

TODO: document what happens with MIXINS, how to override definitions, etc.

Usage

Website integration

This is for people not wanting to use Visual Studio. And it requires that the Umbraco website is running in FullTrust mode. Deploy the Zbu.ModelsBuilder.AspNet project to the Umbraco bin directory, and copy the GeneraleModelsDashboard.ascx wherever you want. Declare that ascx in the dashboard.config file.

Then, when you go to that dashboard, you can click on a button to generate models. Models file go to ~/App_Data/Models, while a special build.models file goes to ~/App_Core. Removing ~/App_Core/build.models disables the models entirely. Note that generating the models imply touching build.models, which in turn imply restarting the app domain.

Console application

Does not exist yet, should come soon.

Visual Studio extension

This is the preferred solution for people running Visual Studio. Install the Visual Studio extension. Then...
Create a file named "Foo.cs" in your solution.
Set the "Custom Tool" for that file to "ZbuModelsBuilder"
Set the "Custom Tool Namespace" for that file to "MyModels" or whatever you want
Save the file...

And it will connect to your Umbraco instance, and generate, underneath that file, all the *.generated.cs files. You will then need to build and deploy your website as usual.

Next

Should document how to customize models, exclude types and properties...

Clone this wiki locally