diff --git a/src/EntityDb.Common/Extensions/ServiceCollectionExtensions.cs b/src/EntityDb.Common/Extensions/ServiceCollectionExtensions.cs index 554e0401..d9c0d8ef 100644 --- a/src/EntityDb.Common/Extensions/ServiceCollectionExtensions.cs +++ b/src/EntityDb.Common/Extensions/ServiceCollectionExtensions.cs @@ -89,15 +89,18 @@ public static void AddEntity(this IServiceCollection serviceCollection) /// /// The service collection. /// The agent's intent for the snapshot repository. - /// If true then snapshots will be synchronously recorded. + /// If true then snapshots will be synchronously recorded. /// The type of the entity. public static void AddEntitySnapshotTransactionSubscriber(this IServiceCollection serviceCollection, - string snapshotSessionOptionsName, bool synchronousMode = false) + string snapshotSessionOptionsName, bool testMode = false) where TEntity : IEntity, ISnapshot { - serviceCollection.AddSingleton(serviceProvider => - EntitySnapshotTransactionSubscriber.Create(serviceProvider, snapshotSessionOptionsName, - synchronousMode)); + serviceCollection.Add + ( + testMode ? ServiceLifetime.Singleton : ServiceLifetime.Scoped, + serviceProvider => EntitySnapshotTransactionSubscriber.Create(serviceProvider, snapshotSessionOptionsName, + testMode) + ); } /// @@ -121,15 +124,18 @@ public static void AddProjection( /// /// The service collection. /// The agent's intent for the snapshot repository. - /// If true then snapshots will be synchronously recorded. + /// If true then snapshots will be synchronously recorded. /// The type of the projection. public static void AddProjectionSnapshotTransactionSubscriber( this IServiceCollection serviceCollection, - string snapshotSessionOptionsName, bool synchronousMode = false) + string snapshotSessionOptionsName, bool testMode = false) where TProjection : IProjection, ISnapshot { - serviceCollection.AddSingleton(serviceProvider => - ProjectionSnapshotTransactionSubscriber.Create(serviceProvider, snapshotSessionOptionsName, - synchronousMode)); + serviceCollection.Add + ( + testMode ? ServiceLifetime.Singleton : ServiceLifetime.Scoped, + serviceProvider => ProjectionSnapshotTransactionSubscriber.Create(serviceProvider, snapshotSessionOptionsName, + testMode) + ); } } diff --git a/src/EntityDb.Common/Transactions/EntitySnapshotTransactionSubscriber.cs b/src/EntityDb.Common/Transactions/EntitySnapshotTransactionSubscriber.cs index f0a9f293..3bc4f2ca 100644 --- a/src/EntityDb.Common/Transactions/EntitySnapshotTransactionSubscriber.cs +++ b/src/EntityDb.Common/Transactions/EntitySnapshotTransactionSubscriber.cs @@ -20,8 +20,8 @@ public EntitySnapshotTransactionSubscriber ( ISnapshotRepositoryFactory snapshotRepositoryFactory, string snapshotSessionOptionsName, - bool synchronousMode - ) : base(synchronousMode) + bool testMode + ) : base(testMode) { _snapshotRepositoryFactory = snapshotRepositoryFactory; _snapshotSessionOptionsName = snapshotSessionOptionsName; @@ -63,10 +63,10 @@ protected override async Task NotifyAsync(ITransaction transaction) } public static EntitySnapshotTransactionSubscriber Create(IServiceProvider serviceProvider, - string snapshotSessionOptionsName, bool synchronousMode) + string snapshotSessionOptionsName, bool testMode) { return ActivatorUtilities.CreateInstance>(serviceProvider, snapshotSessionOptionsName, - synchronousMode); + testMode); } } diff --git a/src/EntityDb.Common/Transactions/ProjectionSnapshotTransactionSubscriber.cs b/src/EntityDb.Common/Transactions/ProjectionSnapshotTransactionSubscriber.cs index 750b4daf..2d1c01ec 100644 --- a/src/EntityDb.Common/Transactions/ProjectionSnapshotTransactionSubscriber.cs +++ b/src/EntityDb.Common/Transactions/ProjectionSnapshotTransactionSubscriber.cs @@ -26,8 +26,8 @@ public ProjectionSnapshotTransactionSubscriber IProjectionStrategy projectionStrategy, ISnapshotRepositoryFactory snapshotRepositoryFactory, string snapshotSessionOptionsName, - bool synchronousMode - ) : base(synchronousMode) + bool testMode + ) : base(testMode) { _projectionStrategy = projectionStrategy; _snapshotRepositoryFactory = snapshotRepositoryFactory; @@ -77,10 +77,10 @@ protected override async Task NotifyAsync(ITransaction transaction) } public static ProjectionSnapshotTransactionSubscriber Create(IServiceProvider serviceProvider, - string snapshotSessionOptionsName, bool synchronousMode) + string snapshotSessionOptionsName, bool testMode) { return ActivatorUtilities.CreateInstance>(serviceProvider, snapshotSessionOptionsName, - synchronousMode); + testMode); } } diff --git a/src/EntityDb.Common/Transactions/TransactionSubscriber.cs b/src/EntityDb.Common/Transactions/TransactionSubscriber.cs index 43d96b7a..23f02a7f 100644 --- a/src/EntityDb.Common/Transactions/TransactionSubscriber.cs +++ b/src/EntityDb.Common/Transactions/TransactionSubscriber.cs @@ -8,15 +8,15 @@ namespace EntityDb.Common.Transactions; /// public abstract class TransactionSubscriber : ITransactionSubscriber { - private readonly bool _synchronousMode; + private readonly bool _testMode; /// /// Constructs a new instance of . /// - /// If true then the task will be synchronously awaited before returning. - protected TransactionSubscriber(bool synchronousMode) + /// If true then the task will be synchronously awaited before returning. + protected TransactionSubscriber(bool testMode) { - _synchronousMode = synchronousMode; + _testMode = testMode; } /// @@ -24,7 +24,7 @@ public void Notify(ITransaction transaction) { var task = Task.Run(async () => await NotifyAsync(transaction).ConfigureAwait(false)); - if (_synchronousMode) + if (_testMode) { task.Wait(); }