liveBook · Manning

OwningComponentBase<T>

As explained in Pro ASP.NET Core 7, Tenth Edition

OwningComponentBase is a Blazor base component class that provides a dependency-injection scope tied to the lifetime of its owning component.

OwningComponentBase<T> is a Blazor base component that gives the component its own dependency-injection scope. Scoped services obtained from that scope live with the component instead of being shared through the request scope. This is particularly useful for keeping an Entity Framework Core context aligned with a component’s lifetime. section 10.4 section 36.3.1

Use the base class according to how many scoped services the component needs:

  • Inherit from OwningComponentBase when several scoped services are required, then obtain them through the inherited ScopedServices IServiceProvider. section 36.3.1
  • Inherit from OwningComponentBase<DataContext> when one scoped service is central; the typed base exposes it through the Service property. section 36.3.1
  • Keep Inject for services that should remain request-scoped; changing the base class does not move Inject-injected services into the component scope. section 36.3.1

A typed editing component

An Editor.razor component can declare `@inherits OwningComponentBase<DataContext>` and use `DataContext? Context => Service`. NavigationManager may still use `[Inject]`, while DataContext comes from the component-owned scope. The route `/forms/edit/{id:long}` then edits data with a context dedicated to that component lifecycle. section 36.3.1

Listing 36.16 Using the typed base in the Editor.razor file in the Blazor/Forms folder
@page "/forms/edit/{id:long}"
@layout EmptyLayout
@inherits OwningComponentBase<DataContext>

<link href="/blazorValidation.css" rel="stylesheet" />
<h4 class="bg-primary text-center text-white p-2">Edit</h4>
<h6 class="bg-info text-center text-white p-2">@FormSubmitMessage</h6>

<!-- ...elements omitted for brevity... -->

@code {

    [Inject]
    public NavigationManager? NavManager { get; set; }
        
    //[Inject]
    DataContext? Context => Service;
        
    [Parameter]
    public long Id { get; set; }
        
    // ...statements omitted for brevity...
}

The complete typed-base listing shows the inheritance declaration, Service-backed context property, injected navigation service, and edit route together. section 36.3.1

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains component scopes, ScopedServices, Service, and Inject limitations, but omits their internal disposal implementation.
Examples
Uses an Editor.razor form, DataContext, NavigationManager, and the /forms/edit/{id:long} route.
What it assumes
Assumes familiarity with Blazor components, Razor inheritance, dependency injection, scoped services, and Entity Framework Core contexts.
What you can do after
Operate/configure component-owned scopes and obtain typed or multiple scoped services without sharing them across components.
How it connects
Connects Blazor components with dependency injection scopes, scoped services, Entity Framework Core, IServiceProvider, and Inject.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.