Appearance
Are you an LLM? You can read better optimized documentation at /api_reference/models.md for this page in Markdown format
Models Reference
This document provides a detailed technical reference for the C# content models within the TheExampleApp.Models namespace. These models serve as the application's strongly-typed representation of content fetched from the Contentful headless CMS. Understanding these models is critical for developing and maintaining features that interact with content.
For a higher-level overview of the modeling strategy, see the Core Features: Models documentation.
Content Models
Content models are the primary C# classes that map directly to content types defined in Contentful. Each model includes a SystemProperties Sys property, which is populated by the Contentful SDK and contains essential metadata like the entry ID, content type, and revision history.
For more details on how these models are populated by the SDK, refer to the Contentful Integration architecture guide.
Course
The Course model represents a single educational course. It aggregates course metadata, descriptive content, and its constituent lessons and categories.
csharp
// TheExampleApp/Models/Course.cs
using System;
using System.Collections.Generic;
using Contentful.Core.Models;
namespace TheExampleApp.Models
{
public class Course
{
public SystemProperties Sys { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public Asset Image { get; set; }
public string ShortDescription { get; set; }
public string Description { get; set; }
public int Duration { get; set; }
public string SkillLevel { get; set; }
public List<Lesson> Lessons { get; set; }
public List<Category> Categories { get; set; }
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Properties
| Property | Type | Description |
|---|---|---|
Sys | SystemProperties | Contentful system metadata for the entry. |
Title | string | The display title of the course. |
Slug | string | The URL-friendly identifier for the course, used in routing. |
Image | Asset | A linked Contentful asset representing the course's hero or thumbnail image. |
ShortDescription | string | A brief summary of the course, suitable for list views or cards. |
Description | string | The full description of the course, likely authored in Markdown and processed by the Markdig library. |
Duration | int | The estimated course duration in minutes. |
SkillLevel | string | The target skill level (e.g., "Beginner", "Intermediate"). |
Lessons | List<Lesson> | A list of linked Lesson entries that comprise the course content. |
Categories | List<Category> | A list of linked Category entries used to classify the course. |
Implementation Details
- Linked Entries: The
LessonsandCategoriesproperties are populated from linked entries in Contentful. The Contentful SDK resolves these links automatically. Developers should be prepared for these lists to benullor empty if no entries are linked in the CMS. - Asset Handling: The
Imageproperty is of typeContentful.Core.Models.Asset. When rendering, you will need to access itsFile.Urlproperty to get the image source URL.
Lesson
The Lesson model defines the structure of an individual lesson within a course. It is composed of a list of modules, allowing for flexible and dynamic content presentation.
csharp
// TheExampleApp/Models/Lesson.cs
using System.Collections.Generic;
using Contentful.Core.Models;
namespace TheExampleApp.Models
{
public class Lesson
{
public SystemProperties Sys { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public List<ILessonModule> Modules { get; set; }
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
Properties
| Property | Type | Description |
|---|---|---|
Sys | SystemProperties | Contentful system metadata for the entry. |
Title | string | The display title of the lesson. |
Slug | string | The URL-friendly identifier for the lesson. |
Modules | List<ILessonModule> | An ordered list of content modules that make up the body of the lesson. See Module Interfaces. |
Category
The Category model is a simple taxonomy entity used to group and filter courses.
csharp
// TheExampleApp/Models/Category.cs
using Contentful.Core.Models;
namespace TheExampleApp.Models
{
public class Category
{
public SystemProperties Sys { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public string Description { get; set; }
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
Properties
| Property | Type | Description |
|---|---|---|
Sys | SystemProperties | Contentful system metadata for the entry. |
Title | string | The name of the category. |
Slug | string | The URL-friendly identifier. |
Description | string | A short description of the category. |
Layout
The Layout model is used to define reusable page structures, such as a homepage or a landing page. Similar to Lesson, it uses a modular composition approach.
csharp
// TheExampleApp/Models/Layout.cs
using System.Collections.Generic;
using Contentful.Core.Models;
namespace TheExampleApp.Models
{
public class Layout
{
public SystemProperties Sys { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public List<ILayoutModule> ContentModules { get; set; }
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
Properties
| Property | Type | Description |
|---|---|---|
Sys | SystemProperties | Contentful system metadata for the entry. |
Title | string | An internal title for identifying the layout in the CMS. |
Slug | string | The URL slug where this layout should be rendered (e.g., /, /about). |
ContentModules | List<ILayoutModule> | An ordered list of modules that build the page. See Module Interfaces. |
Module Interfaces and Classes
The application employs a modular content strategy, allowing content editors to compose pages and lessons from a library of predefined "blocks" or "modules." This is implemented using a set of C# interfaces and corresponding concrete classes.
Core Module Interfaces
These interfaces establish a contract for all content modules and provide a mechanism for type-safe categorization.
csharp
// TheExampleApp/Models/IModule.cs
using Contentful.Core.Models;
namespace TheExampleApp.Models
{
/// <summary>
/// Interface to mark which classes can be used as modules.
/// </summary>
public interface IModule
{
SystemProperties Sys { get; set; }
}
/// <summary>
/// Interface to mark lesson modules.
/// </summary>
public interface ILessonModule : IModule { }
/// <summary>
/// Interface to mark layout modules.
/// </summary>
public interface ILayoutModule : IModule { }
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
IModule: The base interface for all modules. Its only requirement is theSystemProperties Sysproperty, which is essential for the Contentful SDK to identify and process it as a content entry.ILessonModule: A marker interface that inherits fromIModule. It is used to designate modules that are permissible within aLesson.Modulescollection.ILayoutModule: A marker interface that inherits fromIModule. It is used to designate modules that are permissible within aLayout.ContentModulescollection.
This use of marker interfaces follows the Interface Segregation Principle and allows the Lesson and Layout models to enforce which types of modules they can contain.
Concrete Module Implementations
Concrete classes implement ILessonModule or ILayoutModule and map to specific content types in Contentful. The Contentful SDK's ContentTypeResolver is configured to map a Contentful contentTypeId to one of these C# classes. For details on this mapping, see the API Configuration documentation.
While the source files for these classes are not provided here, their structure can be inferred from their names and the overall architecture.
Layout Modules (implementing ILayoutModule)
LayoutCopy: Likely contains a singlestringorRichTextproperty for rendering blocks of text.LayoutHeroImage: Would contain properties for a title, subtitle, backgroundAsset, and perhaps a call-to-action link.LayoutHighlightedCourse: Would contain a linked entry reference to aCoursemodel to feature it on a layout page.
Lesson Modules (implementing ILessonModule)
LessonCopy: Similar toLayoutCopy, used for instructional text within a lesson. The content is likely Markdown.LessonImage: Contains anAssetproperty for an image and possibly astringfor a caption.LessonCodeSnippets: A critical module for a technical course. It would likely have properties forstring Code,string Language(e.g., "csharp", "javascript"), and an optionalstring Caption.
Developer Gotcha: When rendering a list of modules (e.g., Model.Modules in a Razor view), you must check the type of each item in the collection to render the correct partial view.
csharp
// Example in a Razor view
@foreach (var module in Model.Modules)
{
if (module is LessonCodeSnippets code)
{
@await Html.PartialAsync("_CodeSnippetPartial", code)
}
else if (module is LessonCopy copy)
{
@await Html.PartialAsync("_CopyPartial", copy)
}
// ... other module types
}1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
BasePageModel
The BasePageModel is not a content model but a foundational class within the ASP.NET Core application structure. While its source is not provided, it can be inferred to be a custom base class from which all other PageModel classes in the application inherit.
This is a standard design pattern used to provide common functionality and data to all pages without duplicating code.
Likely Responsibilities:
- Shared Layout Data: Populating data required by the
_Layout.cshtmlfile, such as main navigation links, footer content, or site-wide settings. - SEO Metadata: Containing properties for
Title,MetaDescription, and other SEO-related tags that are set on a per-page basis. - User Context: Holding information about the currently logged-in user, if applicable.
- Global Settings: Fetching and providing access to global site settings stored as a specific entry type in Contentful.
For more information on the application's specific page model implementations that likely inherit from this base class, please refer to the Page Models API Reference.