Skip to main content
Table of Contents

Class NpgsqlDbContextServiceCollectionExtensions

Namespace
Microsoft.Extensions.DependencyInjection
Assembly
Stratara.EventSourcing.EntityFrameworkCore.dll

Service-collection extensions that register Npgsql-backed IDbContextFactory<TContext> instances for the Stratara write, read, and identity stores with snake_case naming, pgvector support, and a reduced connection-pool cap suitable for multi-tenant hosts.

public static class NpgsqlDbContextServiceCollectionExtensions
Inheritance
NpgsqlDbContextServiceCollectionExtensions
Inherited Members

Remarks

A connection string that names no maximum pool size gets 15 rather than Npgsql's 100: one host registers several contexts over one database, and several hosts share that database's max_connections. A connection string that names one keeps it, whatever its value. Contexts that share a connection string share its pool, so on a host that runs several roles every role draws from it. The size in effect is logged once per host and database, when the host creates the first context for it.

Methods

AddNpgsqlIdentityDbContextFactory<TDbContext>(IServiceCollection)

Registers an Npgsql-backed IDbContextFactory<TContext> for an identity-store DbContext together with a scoped resolution of the context itself (so ASP.NET Identity can inject it directly) and the default IDbResolver.

public static IServiceCollection AddNpgsqlIdentityDbContextFactory<TDbContext>(this IServiceCollection services) where TDbContext : DbContext, IIdentityDbContext

Parameters

services IServiceCollection

The service collection to add registrations to.

Returns

IServiceCollection

The same IServiceCollection for chaining.

Type Parameters

TDbContext

The concrete identity-store DbContext type.

Examples

Also resolves the context itself as scoped, so ASP.NET Core Identity can inject it directly:

services.AddNpgsqlIdentityDbContextFactory<AppIdentityDbContext>();

AddNpgsqlReadDbContextFactory<TDbContext>(IServiceCollection)

Registers an Npgsql-backed IDbContextFactory<TContext> for a read-store DbContext together with the read-side unit of work over it, and the default IDbResolver if none has been registered yet.

public static IServiceCollection AddNpgsqlReadDbContextFactory<TDbContext>(this IServiceCollection services) where TDbContext : DbContext, IReadDbContext

Parameters

services IServiceCollection

The service collection to add registrations to.

Returns

IServiceCollection

The same IServiceCollection for chaining.

Type Parameters

TDbContext

The concrete read-store DbContext type.

Examples

services.AddNpgsqlReadDbContextFactory<AppReadDbContext>();

Remarks

The IProjectionsUnitOfWork that projections query and write through — and the IReadUnitOfWork it derives from, resolving to the same scoped instance — is registered here as ProjectionsUnitOfWork<TDbContext>. A host that registers its own read-side unit of work, before or after this call, keeps it. The store that keeps the tenants projections have seen deleted is registered here too, as AddStrataraForgottenTenants would.

AddNpgsqlWriteDbContextFactory<TDbContext>(IServiceCollection)

Registers an Npgsql-backed IDbContextFactory<TContext> for a write-store DbContext together with the write-side unit of work over it, and the default IDbResolver if none has been registered yet.

public static IServiceCollection AddNpgsqlWriteDbContextFactory<TDbContext>(this IServiceCollection services) where TDbContext : DbContext, IWriteDbContext

Parameters

services IServiceCollection

The service collection to add registrations to.

Returns

IServiceCollection

The same IServiceCollection for chaining.

Type Parameters

TDbContext

The concrete write-store DbContext type.

Examples

Reads the connection string named defaultdb; with the session and security composites in place, nothing else is needed for the store:

services.AddNpgsqlWriteDbContextFactory<AppWriteDbContext>();

Remarks

This binds the write store to the host: the IWriteUnitOfWork that the event source, the outbox dispatcher and the command worker depend on is registered here, scoped, as WriteUnitOfWork<TDbContext> over this context. A host that registers its own IWriteUnitOfWork — before or after this call — keeps it.

The unit of work also takes the ambient ISessionContextProvider and the ISecureJsonSerializer, which come from AddSessionContext() and AddSecurity() — applied by every worker composite, and to be called explicitly by a host that composes its own role. They are resolved when the unit of work is first used, so the order of the Add* calls does not matter.

The registration also contributes the IStoreConflictDetector that recognises PostgreSQL's unique-violation error, so a duplicate stream version surfaces as a ConcurrencyException. Detectors accumulate: a host that adds one for another provider does not displace this one, and applying this registration twice adds it once.