Skip to content

Commit c219466

Browse files
authored
Merge pull request #6844 from umbraco/modelsbuilder
Added some text to clarify code samples in Modelsbuilder articles
2 parents ede2229 + e0a2b6d commit c219466

File tree

4 files changed

+198
-131
lines changed

4 files changed

+198
-131
lines changed

10/umbraco-cms/reference/templating/modelsbuilder/understand-and-extend.md

+48-32
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
---
2+
description: Understanding and Extending ModelsBuilder in Umbraco
3+
---
24

35

4-
meta.Title: "Understand and Extend Modelsbuilder"
5-
description: "Understand and extend modelsbuilder"
6-
---
6+
# Introduction
77

8+
Umbraco’s Models Builder automatically generates strongly typed models for content types, allowing developers to work with Umbraco data in a structured and efficient manner. This article explains how models are generated, how composition and inheritance work, and best practices for extending models without causing issues.
89

9-
# Understand and Extend
10+
## Models Generation Process
1011

11-
Models are generated as partial classes. In its most basic form, a model for content type `TextPage` ends up in a `TextPage.generated.cs` file and looks like:
12+
Models Builder generates each content type as a partial class. For example, a content type named `TextPage` results in a `TextPage.generated.cs` file with a structure like this:
1213

1314
```csharp
1415
/// <summary>TextPage</summary>
1516
[PublishedModel("textPage")]
1617
public partial class TextPage : PublishedContentModel
1718
{
18-
// helpers
19+
//static helpers
1920
public new const string ModelTypeAlias = "textPage";
2021

2122
public new const PublishedItemType ModelItemType = PublishedItemType.Content;
@@ -28,7 +29,7 @@ public partial class TextPage : PublishedContentModel
2829

2930
private IPublishedValueFallback _publishedValueFallback;
3031

31-
// ctor
32+
//constructor
3233
public TextPage(IPublishedContent content, IPublishedValueFallback publishedValueFallback)
3334
: base(content, publishedValueFallback)
3435
{
@@ -45,7 +46,12 @@ public partial class TextPage : PublishedContentModel
4546
}
4647
```
4748

48-
What is important is the `Header` property. The rest is (a) a constructor and (b) some static helpers to get the `PublishedContentType` and the `PublishedPropertyType` objects:
49+
In the above code:
50+
51+
* The model includes a constructor and static helpers to fetch the content type (`PublishedContentType`) and property type (`PublishedPropertyType`).
52+
* The most important part is the property definition (`Header`), which retrieve values from Umbraco.
53+
54+
You can use helper methods to access content and property types:
4955

5056
```csharp
5157
var contentType = TextPage.GetModelContentType(); // is a PublishedContentType
@@ -54,11 +60,16 @@ var propertyType = TextPage.GetModelPropertyType(x => x.Header); // is a Publish
5460

5561
## Composition and Inheritance
5662

57-
Content type *composition* consists in having content types "inherit" properties from other content types. Contrary to C#, where a class can only inherit from one other class, Umbraco content types can be composed of several other content types.
63+
### Composition
64+
65+
Umbraco content types can be composed of multiple other content types. Unlike traditional C# inheritance, Umbraco allows a content type to inherit properties from multiple sources.
5866

59-
The `TextPage` content type could be composed of the `MetaInfo` content type (and thus inherit properties `Author` and `Keywords`) and of the `PageInfo` content type (and thus inherit properties `Title` and `MainImage`).
67+
For example, a `TextPage` might be composed of:
6068

61-
Each content type that is involved in a composition is generated both as a class and as an interface, and so the `MetaInfo` content type would be generated as (some code has been removed and altered for simplicity's sake):
69+
* **MetaInfo** content type (inherits `Author` and `Keywords` properties).
70+
* **PageInfo** content type (inherits `Title` and `MainImage` properties).
71+
72+
Each content type in a composition is generated both as a class and as an interface. The `MetaInfo` content type would be generated as:
6273

6374
```csharp
6475
// The composition interface
@@ -81,7 +92,7 @@ public partial class MetaInfo : PublishedContentModel
8192
}
8293
```
8394

84-
And the `TextPage` model would be generated as (some code has been removed and altered for simplicity's sake):
95+
And the `TextPage` model would be generated as:
8596

8697
```csharp
8798
public partial class TextPage : PublishedContentModel, IMetaInfo
@@ -91,9 +102,13 @@ public partial class TextPage : PublishedContentModel, IMetaInfo
91102
}
92103
```
93104

94-
A content type *parent* is a tree-related concept: In the Umbraco backoffice, a content type appears underneath its parent, if any. By convention, a content type is always **composed of its parent** and therefore inherits its properties. However, the parent content type is treated differently, and the child content type *directly inherits* (as in C# inheritance) from the parent class.
105+
### Inheritance
106+
107+
In addition to composition, content types can have a parent-child relationship. In the Umbraco backoffice, a content type appears underneath its parent.
95108

96-
Therefore, assuming that the `AboutPage` content type is a direct child of `TextPage`, it would be generated as:
109+
By convention, a content type is always **composed of its parent** and therefore inherits its properties. However, the parent content type is treated differently, and the child content type *directly inherits* (as in C# inheritance) from the parent class.
110+
111+
If `AboutPage` is a child of TextPage, its generated model would inherit directly from `TextPage`:
97112

98113
```csharp
99114
// Note: Inherits from TextPage
@@ -103,9 +118,11 @@ public partial class AboutPage : TextPage
103118
}
104119
```
105120

106-
## Extending
121+
## Extending Models
122+
123+
Since models are partial classes, developers can extend them by adding additional properties.
107124

108-
Because a model is generated as a partial class, it is possible to extend it. That could be by adding a property, by dropping the following code in a `TextPage.cs` file:
125+
For Example:
109126

110127
```csharp
111128
public partial class TextPage
@@ -114,17 +131,17 @@ public partial class TextPage
114131
}
115132
```
116133

117-
Models builder does not take a custom partial class into account when generating the models. This means that if a custom partial class, inherits from a base class, tries to provide a constructor with the same signature, or implements a generated property, it will cause compilation errors.
134+
Models Builder does not recognize custom partial classes during regeneration. If your custom class conflicts with the generated class (e.g., overriding a constructor), it will cause compilation errors.
118135

119-
Furthermore a generated model will always be instantiated with its default constructor, so if an overloading constructor is created it will never be used.
136+
Overloaded constructors will not be used because models are always instantiated using the default constructor.
120137

121-
For more complex partial classes, you'll have to use the full version of the [Models Builder](https://github.com/zpqrtbnk/Zbu.ModelsBuilder).
138+
For more complex customizations, use the full version of [Models Builder](https://github.com/zpqrtbnk/Zbu.ModelsBuilder).
122139

123-
## Best Practices
140+
## Best Practices for Extending Models
124141

125-
Extending models should be used to add stateless, local features to models, and *not* to transform *content* models into view models or manage trees of content.
142+
Extending models should be used to add stateless, local features to models. It should not be used to transform *content* models into view models or manage trees of content.
126143

127-
### Example of good practice
144+
### Good practices
128145

129146
A customer has "posts" that has two "release date" properties. One is a true date picker property and is used to specify an actual date and to order the posts. The other is a string that is used to specify dates such as "Summer 2015" or "Q1 2016". Alongside the title of the post, the customer wants to display the text date, if present, else the actual date. If none of those are present, the Umbraco update date should be used. Keep in mind that each view can contain code to deal with the situation, but it is much more efficient to extend the `Post` model:
130147

@@ -151,7 +168,7 @@ A customer has "posts" that has two "release date" properties. One is a true dat
151168
}
152169
```
153170

154-
And to simplify the view as:
171+
Simplified view:
155172

156173
```csharp
157174
<div class="title">
@@ -160,13 +177,12 @@ And to simplify the view as:
160177
</div>
161178
```
162179

163-
### Example of bad practice
180+
### Bad practices
164181

165182
Because, by default, the content object is passed to views, one can be tempted to add view-related properties to the model. Some properties that do *not* belong to a *content* model would be:
166183

167-
* A `HomePage` property that would walk up the tree and return the "home page" content item
168-
* A `Menu` property that would list the content items to display in a top menu
169-
* Etc.
184+
* A `HomePage` property that retrieves the "home page" content item.
185+
* A `Menu` property that lists navigation items.
170186

171187
Generally speaking, anything that is tied to the current request, or that depends on more than the modeled content, is a bad idea. There are much cleaner solutions, such as using true *view model* classes that would be populated by a true controller and look like:
172188

@@ -179,17 +195,17 @@ public class TextPageViewModel
179195
}
180196
```
181197

182-
One can also extend Umbraco's views to provide a special view helper that would give access to important elements of the website, so that views could contain code such as:
198+
One can also extend Umbraco's views to provide a special view helper that gives access to important elements of the website:
183199

184200
```csharp
185201
<a href="@MySite.HomePage.Url">@MySite.HomePage.Title</a>
186202
```
187203

188-
### Example of ugly practice
204+
### Ugly practices
189205

190-
The scope and life-cycle of a model is *not specified*. In other words, you don't know whether the model exists only for you and for the context of the current request, or if it is cached by Umbraco and shared by all requests.
206+
The model's scope and lifecycle are *unspecified*. It may exist only for your request or be cached and shared across all requests.
191207

192-
As a consequence, the following code has a major issue: the `TextPage` model "caches" an instance of the `HomePageDocument` model that will never be updated if the home page is re-published.
208+
The code has a major issue: the `TextPage` model caches a `HomePageDocument` model that will not update when the home page is re-published.
193209

194210
```csharp
195211
private HomePageDocument _homePage;
@@ -206,4 +222,4 @@ public HomePageDocument HomePage
206222
}
207223
```
208224

209-
As a rule of thumb, models should never, *ever* reference and cache other models.
225+
As a rule of thumb, models should never reference and cache other models.

0 commit comments

Comments
 (0)