Builder Lifecycle

Understand when a builder starts, writes, and completes its output.

Overview

The builder lifecycle is one of the central concepts of PageBuilder. A builder is not just a passive wrapper around HTML. It participates in a rendering process with a defined beginning, an output phase, and a completion phase.

This lifecycle is what makes builders reusable, composable, and predictable inside the PageBuilder rendering pipeline.

Lifecycle diagram

The diagram shows the two rendering styles that PageBuilder builders commonly use: scoped rendering with Begin() and Dispose() , and page model enrichment during rendering.

…loading…

Why lifecycle matters

Without a clear lifecycle, a rendering system quickly becomes difficult to reason about. Components start writing output at arbitrary times, nested rendering becomes fragile, and debugging rendering order becomes harder.

PageBuilder avoids this by giving builders a disciplined rendering model. This model allows a builder to behave consistently whether it is used in Razor, in C#, or inside a larger builder composition.

The core rendering contract

At the lowest level, builders render themselves through WriteTo(...) . This is the base output contract used by the framework. WriteTo(...) is responsible for:

Applying the rendering flow consistently
Triggering debug metadata when enabled
Delegating the actual HTML generation to WriteToCore(...)

This means builders do not generally write their output by directly assembling arbitrary strings from outside the lifecycle. They render themselves through the framework contract.

Begin and Dispose

Some builders represent a structural region rather than a single atomic element. In those cases, the most natural rendering pattern is a scoped lifecycle based on Begin() and Dispose() . This pattern is used when a builder needs to:

Write an opening structure first
Allow nested content to be rendered in between
Write a closing structure when the scope ends
Using blocks

This makes builders fit naturally inside using blocks. Begin() writes the opening structure, nested content is rendered, then Dispose() writes the closing structure. This is especially useful for container-like builders and region builders.

Render and WriteTo

Not every builder needs a scoped lifecycle. Some builders are atomic and can render themselves in a single step. In those cases, Render() or WriteTo(...) is often sufficient.

WriteTo(...) is the low-level output contract
Render() is often a convenience method
Begin() and Dispose() are used when a builder defines a rendering scope

Lifecycle and nesting

The builder lifecycle is especially important when builders are nested. A parent builder may open a region, a child builder may render inside it, and both must still produce correct and predictable output. A consistent lifecycle ensures that:

Opening and closing tags are emitted in the right order
Nested rendering stays structurally valid
Page-level registration can still happen during rendering
Components remain understandable even when composition becomes deeper

Lifecycle and PageInformation

A builder does not only generate visible HTML. During its lifecycle, it may also enrich the shared PageInformation instance used by the rendering pipeline. For example, a builder may register:

A script dependency
A stylesheet
Inline JavaScript
Inline CSS
Other page-level state

This means the lifecycle of a builder is not limited to writing markup. It can also contribute to the final document model while it renders.

Recommended usage

Use Begin() and Dispose() for structural builders
Use WriteTo(...) as the rendering contract
Use Render() as a convenience, not as a replacement for the lifecycle model
Keep builders aligned with the shared page model when they need to register dependencies
Avoid hidden or unpredictable rendering side effects

Continue with BodyBuilder

Learn how BodyBuilder controls the body shell within the pipeline.

Layout & BodyBuilder →