Path: redb/redb.Templates/templates/redb-blazor/Services/RedbWork.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Templates/templates/redb-blazor/Services/RedbWork.cs
----
using redb.Core;
namespace RedbBlazor.Services;
///
/// Runs one unit of work on its own .
///
///
/// In Blazor Server a scoped service lives as long as the user's circuit, and two event handlers of
/// one page can be in flight at the same time. An is one connection and
/// does not take parallel calls, so components do not inject it: each operation opens a scope, uses
/// the service of that scope and disposes it. This is the same shape as IDbContextFactory in EF Core.
///
public sealed class RedbWork(IServiceScopeFactory scopes)
{
public async Task RunAsync(Func> work)
{
await using var scope = scopes.CreateAsyncScope();
return await work(scope.ServiceProvider.GetRequiredService());
}
public async Task RunAsync(Func work)
{
await using var scope = scopes.CreateAsyncScope();
await work(scope.ServiceProvider.GetRequiredService());
}
}