liveBook · Manning

Service property

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

The Service property is the convenience property on OwningComponentBase<T> that exposes the scoped service of type T.

Service is the convenience property supplied by OwningComponentBase<T>. In a Blazor component inheriting OwningComponentBase<DataContext>, Service exposes the component's scoped DataContext without resolving it manually. section 36.3.1

This typed base creates a component-specific scope, isolating Entity Framework Core changes from other components that share Blazor's persistent connection. The component can access its context through Service and avoid resolving it from ScopedServices. section 36.3.1

Using Service in an editor component

Declare @inherits OwningComponentBase<DataContext>, then expose the context with `DataContext? Context => Service;`. The editor can use Context for its data operations while NavigationManager remains injected normally. 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...
}

Shows the typed OwningComponentBase<DataContext> declaration and the Context property mapped directly to Service. section 36.3.1

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

How deep it goes
Demonstrates the property in a typed base-class implementation but defers its internal resolution mechanics.
Examples
Uses an Editor.razor component exposing DataContext through Service.
What it assumes
Assumes familiarity with Blazor components, dependency injection, scoped services, and Entity Framework Core contexts.
What you can do after
Operate a component-scoped DataContext through Service instead of resolving it manually from ScopedServices.
How it connects
Connects OwningComponentBase<T>, scoped services, dependency injection, Blazor components, and Entity Framework Core.

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.