Path: redb-route/redb.Route.Sql/Connection/ISqlConnectionFactory.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.Sql/Connection/ISqlConnectionFactory.cs
----
using System.Data.Common;
using redb.Route.Abstractions;
namespace redb.Route.Sql.Connection;
///
/// Abstraction for creating instances.
/// Implementations may provide pooling, read/write splitting, or failover.
///
///
/// The sql: producers take their connection through . Its default body opens a connection
/// of the endpoint's own with , so a factory that implements only that method works as
/// before. A factory that lends a connection someone else owns (dataSource=redb: the connection a redb service
/// works on, in the transaction redb's commands run in) implements and
/// .
///
public interface ISqlConnectionFactory
{
/// Creates an opened database connection.
/// Hint for read/write split: true = read replica, false = primary.
/// Cancellation token.
/// An opened .
Task CreateConnectionAsync(bool readOnly = false, CancellationToken ct = default);
///
/// True when lends a connection the endpoint does not own. The endpoint then begins no
/// transaction of its own (it runs in the lender's transaction, or in autocommit when there is none), keeps no reader
/// open past the step (outputType=StreamList is refused), and is refused where no exchange holds the lease (the
/// polling consumer, the idempotent and claim-check repositories). Default: false.
///
bool LendsConnection => false;
///
/// The factory a data source name with a qualifier stands for: dataSource=redb:orders finds the factory
/// registered under redb and asks it for orders. Called when the endpoint resolves its factory (the
/// producer's start, and every exchange), with null for the bare name. Throws when the qualifier names nothing the
/// factory knows, so a wrong name fails at start. Default: this factory for the bare name, a refusal for a qualifier.
///
/// The part of the data source name after the first :; null for the bare name.
ISqlConnectionFactory ForQualifier(string? qualifier)
=> qualifier is null
? this
: throw new InvalidOperationException(
$"{GetType().Name} takes no qualifier, and the data source name asks for '{qualifier}'. Register a " +
$"connection factory under the whole name, or remove ':{qualifier}' from the data source.");
///
/// The connection one sql: step runs on, given back when the lease is disposed. Default: a connection of the
/// step's own from , closed when the lease is disposed.
///
/// The exchange the step runs for; a lending factory finds the lender through it.
/// Hint for read/write split, as in .
/// Cancellation token.
async Task LeaseAsync(IExchange exchange, bool readOnly = false, CancellationToken ct = default)
=> SqlConnectionLease.Owned(await CreateConnectionAsync(readOnly, ct).ConfigureAwait(false));
}