Path: redb/redb.Core/RedbScope.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.Core/RedbScope.cs
----
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace redb.Core;
///
/// A scope opened from a service () and the service of the same database
/// inside it. The holder owns the scope: disposing it ends the service and returns its connection.
///
public sealed class RedbScope : IDisposable, IAsyncDisposable
{
private readonly IServiceScope? _scope;
///
/// Wraps with the scope that owns it. A null scope is for a service owned elsewhere
/// (a test double); disposing then does nothing.
///
public RedbScope(IRedbService service, IServiceScope? scope)
{
ArgumentNullException.ThrowIfNull(service);
Service = service;
_scope = scope;
}
/// The service inside the scope.
public IRedbService Service { get; }
/// The scope's provider, for other scoped services of the same unit of work; null without a scope.
public IServiceProvider? ServiceProvider => _scope?.ServiceProvider;
///
public void Dispose() => _scope?.Dispose();
///
public ValueTask DisposeAsync()
{
if (_scope is IAsyncDisposable asyncScope)
return asyncScope.DisposeAsync();
_scope?.Dispose();
return default;
}
}