Skip to main content
Table of Contents

Class TestEventStoreServiceCollectionExtensions

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

Wires the real Stratara event-sourcing write stack against a shared in-memory SQLite database, using the in-memory doubles from Stratara.Testing for the cross-cutting dependencies.

public static class TestEventStoreServiceCollectionExtensions
Inheritance
TestEventStoreServiceCollectionExtensions
Inherited Members

Methods

AddStrataraTestingEventStore<TWriteDbContext>(IServiceCollection, SqliteConnection, Guid)

Register the event-sourcing write stack (IEventSource, IAggregationService, snapshots, the EF Core write store) over sharedConnection, plus an InMemoryKeyStore, a TestSessionContextProvider, and a RecordingEventBundleOutboxDispatcher. Register your aggregates with AddAggregatesFromAssemblyContaining<T>() so event payload types deserialize. A duplicate stream version surfaces as ConcurrencyException here as it does on PostgreSQL, through the SQLite IStoreConflictDetector this call registers.

public static IServiceCollection AddStrataraTestingEventStore<TWriteDbContext>(this IServiceCollection services, SqliteConnection sharedConnection, Guid defaultTenantId) where TWriteDbContext : DbContext, IWriteDbContext

Parameters

services IServiceCollection

The service collection.

sharedConnection SqliteConnection

An open SQLite connection kept alive for the test's lifetime. It must stay open so every DbContext minted by the unit of work shares the same in-memory database. The caller owns it.

defaultTenantId Guid

The tenant the preset session context is scoped to.

Returns

IServiceCollection

The same service collection for chaining.

Type Parameters

TWriteDbContext

The concrete write DbContext (e.g. StrataraTestWriteDbContext).

Examples

The connection is shared so the in-memory database outlives a single context. Register the aggregates too, or persisted event payloads will not resolve:

var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
services.AddStrataraTestingEventStore<AppWriteDbContext>(connection, Guid.NewGuid());
services.AddAggregatesFromAssemblyContaining<Account>();

Exceptions

InvalidOperationException

Thrown when the service collection carries a host environment, or the environment variables state one, naming anything other than Development. Where no environment is stated at all — an ordinary unit test — the call is allowed.

AddStrataraTestingEventStore<TWriteDbContext>(IServiceCollection, string, Guid, Action<DbContextOptionsBuilder>?)

Register the same write stack as AddStrataraTestingEventStore<TWriteDbContext>(IServiceCollection, SqliteConnection, Guid), with every context opening its own connection from connectionString and configureContext applied to the options after the provider — for a test whose store is used from several threads at once, such as a silo's, and for a write context that needs an interceptor.

public static IServiceCollection AddStrataraTestingEventStore<TWriteDbContext>(this IServiceCollection services, string connectionString, Guid defaultTenantId, Action<DbContextOptionsBuilder>? configureContext = null) where TWriteDbContext : DbContext, IWriteDbContext

Parameters

services IServiceCollection

The service collection.

connectionString string

A SQLite connection string. For an in-memory database shared by the contexts, name it and use a shared cache — Data Source=name;Mode=Memory;Cache=Shared — and keep one connection to it open for the test's lifetime; the database is gone when the last connection closes.

defaultTenantId Guid

The tenant the preset session context is scoped to.

configureContext Action<DbContextOptionsBuilder>

Applied to every write context's options after the SQLite provider, or null.

Returns

IServiceCollection

The same service collection for chaining.

Type Parameters

TWriteDbContext

The concrete write DbContext (e.g. StrataraTestWriteDbContext).

Examples

var connectionString = $"Data Source=store-{Guid.NewGuid():N};Mode=Memory;Cache=Shared";
using var keeper = new SqliteConnection(connectionString);
keeper.Open();
services.AddStrataraTestingEventStore<AppWriteDbContext>(
    connectionString,
    Guid.NewGuid(),
    options => options.LogTo(Console.WriteLine));

Exceptions

ArgumentException

connectionString is null, empty or white space.

InvalidOperationException

Thrown when the service collection carries a host environment, or the environment variables state one, naming anything other than Development.