Path: redb-route/redb.Route.Firebase/Firestore/FirestoreComponent.cs
Snapshot: redb-route, 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-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Firebase/Firestore/FirestoreComponent.cs
----
using Google.Cloud.Firestore;
using redb.Route.Abstractions;
using redb.Route.Extensions;
using redb.Route.Core;
namespace redb.Route.Firebase;
///
/// Firestore component registered with scheme fstore.
/// Creates endpoints for CRUD operations and realtime listeners.
///
internal sealed class FirestoreComponent : ComponentBase
{
///
public override string Scheme => "fstore";
/// Shared credential provider. Set by DI registration or manually.
internal IFirebaseCredentialProvider? CredentialProvider { get; set; }
///
public override IEndpoint CreateEndpoint(EndpointUri uri)
{
ArgumentNullException.ThrowIfNull(uri);
var options = new FirestoreEndpointOptions();
options.BindFromUri(uri.RawParameters);
options.Validate();
return new FirestoreEndpoint(uri, this, options);
}
}
///
/// Firestore endpoint. Creates producers for CRUD operations and consumers for realtime listeners.
/// The URI path specifies the collection (e.g. fstore://users, fstore://users/uid/orders).
///
internal sealed class FirestoreEndpoint : EndpointBase, IDisposable
{
private FirestoreDb? _db;
private readonly SemaphoreSlim _lock = new(1, 1);
internal FirestoreEndpoint(EndpointUri uri, FirestoreComponent component, FirestoreEndpointOptions options)
: base(uri, component, options) { }
/// Collection path from the URI.
internal string CollectionPath => Uri.Path;
/// The owning Firestore component.
internal FirestoreComponent FirestoreComponent => (FirestoreComponent)Component;
/// Typed options for external access.
internal FirestoreEndpointOptions EndpointOptions => Options;
/// Gets or creates a shared client.
internal async Task GetOrCreateDbAsync(CancellationToken ct = default)
{
if (_db is not null) return _db;
await _lock.WaitAsync(ct).ConfigureAwait(false);
try
{
if (_db is not null) return _db;
var provider = ResolveCredentialProvider();
_db = provider.GetFirestoreDb(Options.ProjectId, Options.CredentialPath, Options.DatabaseId);
return _db;
}
finally
{
_lock.Release();
}
}
///
public override IProducer CreateProducer()
=> new FirestoreProducer(this, Options);
///
public override IConsumer CreateConsumer(IProcessor processor)
{
ArgumentNullException.ThrowIfNull(processor);
// ${...} in Where is producer-Query-only: the consumer query is built once per
// subscription/poll loop — there is no exchange to resolve the template against.
if (Options.Where?.Contains("${") == true)
throw new InvalidOperationException(
"Where with ${...} expressions is supported only on the producer Query operation: " +
"the consumer query is built once per subscription, there is no exchange to resolve against.");
return Options.Realtime
? new FirestoreConsumer(this, processor, Options)
: new FirestorePollingConsumer(this, processor, Options);
}
///
public void Dispose()
{
_lock.Dispose();
}
private IFirebaseCredentialProvider ResolveCredentialProvider()
{
// A set-but-unknown name fails loud — never a silent fallback.
if (!string.IsNullOrEmpty(Options.ConnectionFactory))
return FirestoreComponent.Context
.GetRequiredFromRegistry(Options.ConnectionFactory);
return FirestoreComponent.CredentialProvider
?? throw new InvalidOperationException(
"No IFirebaseCredentialProvider available. Register via AddRedbRouteFirebase() or set ConnectionFactory.");
}
}