liveBook · Manning

Transient lifetime

As explained in .NET MAUI in Action

A transient scope is a dependency-injection lifetime in which a new service instance is created each time the service is requested.

A transient lifetime creates a new dependency-injection service instance each time it is requested. In a .NET MAUI UI app, this is useful when each page or view model should begin with clean state. section 8.2

Use transient registration when:

  • A page should be recreated for each request, avoiding stale values and preserving state elsewhere. section 8.2
  • A view model should provide a clean instance alongside a newly requested page. section 8.2
  • A navigation style expects fresh pages, unlike Shell navigation, which commonly keeps pages as singletons. section 8.2

Registering a transient page

Register a modal LoginPage manually in MauiProgram.cs with builder.Services.AddTransient<LoginPage>();. Resolve it through dependency injection when navigation needs a new page. section 8.2.1

Table 8.2 Scope lifetimes for different dependencies in a .NET MAUI app (view table figure)

Dependency type

Scope

Reasoning

Pages

Transient

We should expect a new instance of a page every time we request one. Persisting values across different instances of a page could cause problems, so we’re better off persisting state elsewhere and using transient instances of pages.

ViewModels

Transient

Just like pages, our ViewModels should be transient, so we know we’ve got a clean instance every time we get a new ViewModel.

Services

Singleton

We should only expect one instance of a service for the running lifetime of an app. Instantiating multiple copies of a database or a service responsible for communicating with an API is a waste of resources and can lead to data conflicts. Services should be singletons and can therefore be where we persist app-wide state. We can inject these singleton instances into ViewModels.

Compare recommended lifetimes for pages, view models, and services in a .NET MAUI app. section 8.2

How .NET MAUI in Action covers it

How deep it goes
Explains lifetime trade-offs, navigation expectations, state concerns, and the AddTransient registration step.
Examples
Demonstrates transient registration with LoginPage in MauiProgram.cs and compares it with Shell-managed pages and singleton services.
What it assumes
Assumes familiarity with dependency injection registration and distinguishes UI lifetimes from ASP.NET Core request-scoped lifetimes.
What you can do after
Operate/configure .NET MAUI dependency injection by registering a page with AddTransient and selecting lifetimes by navigation and state needs.
How it connects
Contrasts transient lifetime with singleton lifetime and scoped lifetime while connecting pages, view models, services, and Shell navigation.

Also explained in

Compare how 2 books explain transient lifetime →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET MAUI in Action. The section links point to the parts of the book it draws on.