Skip to content
Stephan edited this page Nov 1, 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.

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

The models inheritance tree is respected, ie models inherit from each other if required. Mixins are represented by interfaces.

What it does not

This is a "code-after" approach ie it only generates code from models that already exist in Umbraco. It is not generating models based on code, that would be "code-first". You'd want to look at Lars-Erik Umbraco.CodeGen project, which glue together Kevin's uSync and models generation.

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 GenerateModelsDashboard.ascx wherever you want. Declare that ascx in the dashboard.config file.

Alternatively, once you've built the project, there will be an Umbraco package in the pkg directory below the AspNet project, that you can install.

Note that the user running the generator must have permission to access the developer section.

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...
Go to Tools | Options | Zbu | Models Builder and set the database connection string and provider
Create a file named "BuildModels.cs" (or anything else) 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 or leave it blank
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...

Because generated classes are partial, you can extend them with your own partial to implement whatever you want.

To use a different class name (default being derived from the alias):

using Zbu.ModelsBuilder;
namespace MyModels
{
  // alias is myModelType but class should be named MyModel
  [PublishedContentModel("myModelType")]
  public partial class MyModel
  {
  }
}

To exclude a type from the generation, just make sure that a file containing the following is present in the appropriate directory:

using Zbu.ModelsBuilder;
[assembly: IgnoreContentType("alias")]

To exclude a property from the generation, two solutions. Either mark the type itself:

using Zbu.ModelsBuilder;
namespace MyModels
{
  [IgnorePropertyTypeType("alias")]
  public partial class MyModel
  {
  }
}

Or mark a property, usually because you are providing your own implementation:

using Zbu.ModelsBuilder;
namespace MyModels
{
  public partial class MyModel
  {
    [IgnorePropertyTypeType("alias")]
    public string MyProperty { get { return this.GetPropertyValue<string>("alias"); } }
  }
}