Path: redb-route/redb.Route.Sql/Extensions/ServiceCollectionExtensions.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/Extensions/ServiceCollectionExtensions.cs
----
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using redb.Route.Extensions;
using redb.Route.Sql.Connection;
namespace redb.Route.Sql;
///
/// Extension methods for registering the SQL transport in a DI container.
///
public static class ServiceCollectionExtensions
{
///
/// Registers the in the route context so that
/// sql: URIs are resolved.
/// Named data sources are registered in the context registry via
/// context.AddToRegistry(name, ISqlConnectionFactory).
/// The method can be called more than once (one module, one call): every call adds its data sources and named queries
/// to the same component and the same registry.
///
///
/// services.AddRedbRoute(route =>
/// {
/// route.Services.AddRedbRouteSql(sql =>
/// {
/// sql.AddDataSource("main", opts =>
/// {
/// opts.ConnectionString = "Server=...";
/// opts.ProviderName = "Microsoft.Data.SqlClient";
/// });
/// });
/// route.AddRouteBuilder<MyRoutes>();
/// });
///
///
///
public static IServiceCollection AddRedbRouteSql(
this IServiceCollection services,
Action? configure = null)
{
// One registry per container: a later call registers into it instead of replacing it, so the named queries of
// every call resolve. A name registered twice fails right here, in the second call.
var registry = services.FirstOrDefault(d => d.ServiceType == typeof(ISqlNamedQueryRegistry))?.ImplementationInstance
as ISqlNamedQueryRegistry;
if (registry is null)
{
registry = new SqlNamedQueryRegistry();
services.AddSingleton(registry);
}
var builder = new SqlConfigurationBuilder(services, registry);
configure?.Invoke(builder);
services.TryAddSingleton();
// Capture data sources from builder
var dataSources = builder.BuildDataSources();
// IRouteContextConfigurator is applied by RouteHostedService at startup --
// the correct registration hook (a lazy marker singleton never fires).
services.AddRouteContextConfigurator((sp, context) =>
{
if (!context.HasComponent("sql"))
context.AddComponent(sp.GetRequiredService());
// The producer and the consumer resolve ref: queries from the context's services.
context.AddService(typeof(ISqlNamedQueryRegistry), sp.GetRequiredService());
// Register all named data sources in the context registry
foreach (var (name, factory) in dataSources)
context.AddToRegistry(name, factory);
});
return services;
}
}
/// Fluent builder for configuring the SQL connector.
public sealed class SqlConfigurationBuilder
{
private readonly IServiceCollection _services;
private readonly List> _dataSources = [];
private readonly ISqlNamedQueryRegistry _queryRegistry;
internal SqlConfigurationBuilder(IServiceCollection services, ISqlNamedQueryRegistry queryRegistry)
{
_services = services;
_queryRegistry = queryRegistry;
}
/// The service collection.
public IServiceCollection Services => _services;
///
/// Adds a named data source that will be resolved via dataSource=name in SQL URIs.
///
public SqlConfigurationBuilder AddDataSource(string name, Action configure)
{
var options = new SqlConnectionOptions();
configure(options);
var factory = new SqlConnectionFactory(options);
_dataSources.Add(new(name, factory));
return this;
}
///
/// Adds a named data source with a pre-built connection factory.
///
public SqlConfigurationBuilder AddDataSource(string name, ISqlConnectionFactory factory)
{
_dataSources.Add(new(name, factory));
return this;
}
///
/// Registers a named query for reuse via ref:queryName protocol.
///
/// The name is already registered, by this call or an earlier one.
public SqlConfigurationBuilder AddNamedQuery(string name, string sql)
{
_queryRegistry.Register(name, sql);
return this;
}
internal IReadOnlyList> BuildDataSources() => _dataSources;
}