CodeGarden 10: The sixth annual Umbraco Developer Conference
June 23-25th 2010 - free ASP.NET MVC pre-conference. Register today!

Understanding the generated classes

Pages

DocType classes

The generated .NET class representations for the DocTypes are named from the Alias of the DocType (which is normalized into being Pascal Cased and with spaces/ special characters/ etc stripped out) and inherit from Umbraco.Core.Linq.DocTypeBase class (which ships as part of the LINQ to Umbraco core library).
The only exemption to the rule are nested DocTypes, in this case the .NET classes will reflect the Umbraco DocType inheritance.
All properties of the Umbraco DocType are translated into .NET with their .NET representation being determined from the database type of the Umbraco DataType.
There is a slight exception to the rule, with the Yes/ No DataType generating both an integer property and a Boolean property (which is read-only).
In addition to having the properties of the DocType translated to properties of the .NET class the class also has collections which represent the allowed children.

Designing DocTypes for LINQ to Umbraco

The .NET classes generated from LINQ to Umbraco aim to have as simple and expected operation. All the data which is kept against the .NET class is gathered from the Umbraco database, and the data entered when creating DocTypes.
The name of the class is generated from the Alias of the DocType, but it is put through a normalizer.
Since the DataContext will generate with collections for the various DocTypes within the Umbraco site it will also pluralize the collection (unless the –disablePluralization flag is specified). When designing DocTypes you should not pluralize the DocType alias. Doing so will produce a non-compiling .NET class file.

DocType Normalizer

The DocType normalizer will turn the alias into a Pascal-Case name. The Pascal-Casing is done using spaces and underscores.
This means that when designing a DocType it is best to design its alias to have spaces or underscores to break up the “words” of the alias.
The normalizer will also look at the special characters which may have been included in the alias (ie, non-alpha characters) and they will be removed.
Some considerations are:
• Use underscores to space out the words of the DocType Alias
• Keep aliases in all lowercase
• Do not pluralize aliases
• Do not start aliases with numbers
• Avoid using special characters in the alias

 

Serialization

All classes which are generated support serialization via the DataContractAttribute which was introduced as part of the .NET 3.0 framework release. This will require the project to have a reference to the System.Runtime.Serialization assembly.

By supporting serialization via DataContract the generated classes can easily be used with WCF or the new DataContract Serializers (such as the DataContractJsonSerializer), making the data easier to work with from a JSON level.

 

Access Modifiers

All generated classes are defined as public, partial and un-sealed, meaning that the classes can be exposed between assemblies and inherited or extended without touching the generated file.

 

Code Documentation

The generated classes will read some of the meta data provided within Umbraco when the Document Type and Properties are defined. For example, the Description attribute is used to provide the <summary /> XML tags, which help to provide intellisense and documentation for the classes.

 

Custom Attributes

There are a few custom attributes which are added to both the class and the properties of the class.

For complete information about the custom attirubtes and their usage refer to the Umbraco.Linq.Core wiki section.

 

Example generated class

    /// <summary>
/// An item for the RSS feed
/// </summary>
[UmbracoInfo("rss_item")]
[System.Runtime.Serialization.DataContractAttribute()]
[DocType()]
public partial class RssItem : DocTypeBase
{

private string _Description;

private string _Link;

private System.DateTime _PublishDate;

private string _Content;

public RssItem()
{
}

/// <summary>
///
/// </summary>
[UmbracoInfo("description", DisplayName = "Description", Mandatory = true)]
[Property()]
public virtual string Description
{
get
{
return this._Description;
}
set
{
if ((this._Description != value))
{
this.RaisePropertyChanging();
this._Description = value;
this.RaisePropertyChanged("Description");
}
}
}

/// <summary>
///
/// </summary>
[UmbracoInfo("link", DisplayName = "Link", Mandatory = true)]
[Property()]
public virtual string Link
{
get
{
return this._Link;
}
set
{
if ((this._Link != value))
{
this.RaisePropertyChanging();
this._Link = value;
this.RaisePropertyChanged("Link");
}
}
}

/// <summary>
///
/// </summary>
[UmbracoInfo("publish_date", DisplayName = "Publish Date", Mandatory = true)]
[Property()]
public virtual System.DateTime PublishDate
{
get
{
return this._PublishDate;
}
set
{
if ((this._PublishDate != value))
{
this.RaisePropertyChanging();
this._PublishDate = value;
this.RaisePropertyChanged("PublishDate");
}
}
}

/// <summary>
///
/// </summary>
[UmbracoInfo("content", DisplayName = "Content", Mandatory = true)]
[Property()]
public virtual string Content
{
get
{
return this._Content;
}
set
{
if ((this._Content != value))
{
this.RaisePropertyChanging();
this._Content = value;
this.RaisePropertyChanged("Content");
}
}
}