Skip to content

Commit

Permalink
bugfix: transaction subscribers need the same service lifetime as rep…
Browse files Browse the repository at this point in the history
…ository factories
  • Loading branch information
the-avid-engineer committed Apr 6, 2022
1 parent f4f12d7 commit 12cc8da
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
26 changes: 16 additions & 10 deletions src/EntityDb.Common/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,18 @@ public static void AddEntity<TEntity>(this IServiceCollection serviceCollection)
/// </summary>
/// <param name="serviceCollection">The service collection.</param>
/// <param name="snapshotSessionOptionsName">The agent's intent for the snapshot repository.</param>
/// <param name="synchronousMode">If <c>true</c> then snapshots will be synchronously recorded.</param>
/// <param name="testMode">If <c>true</c> then snapshots will be synchronously recorded.</param>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
public static void AddEntitySnapshotTransactionSubscriber<TEntity>(this IServiceCollection serviceCollection,
string snapshotSessionOptionsName, bool synchronousMode = false)
string snapshotSessionOptionsName, bool testMode = false)
where TEntity : IEntity<TEntity>, ISnapshot<TEntity>
{
serviceCollection.AddSingleton<ITransactionSubscriber>(serviceProvider =>
EntitySnapshotTransactionSubscriber<TEntity>.Create(serviceProvider, snapshotSessionOptionsName,
synchronousMode));
serviceCollection.Add<ITransactionSubscriber>
(
testMode ? ServiceLifetime.Singleton : ServiceLifetime.Scoped,
serviceProvider => EntitySnapshotTransactionSubscriber<TEntity>.Create(serviceProvider, snapshotSessionOptionsName,
testMode)
);
}

/// <summary>
Expand All @@ -121,15 +124,18 @@ public static void AddProjection<TProjection, TProjectionStrategy>(
/// </summary>
/// <param name="serviceCollection">The service collection.</param>
/// <param name="snapshotSessionOptionsName">The agent's intent for the snapshot repository.</param>
/// <param name="synchronousMode">If <c>true</c> then snapshots will be synchronously recorded.</param>
/// <param name="testMode">If <c>true</c> then snapshots will be synchronously recorded.</param>
/// <typeparam name="TProjection">The type of the projection.</typeparam>
public static void AddProjectionSnapshotTransactionSubscriber<TProjection>(
this IServiceCollection serviceCollection,
string snapshotSessionOptionsName, bool synchronousMode = false)
string snapshotSessionOptionsName, bool testMode = false)
where TProjection : IProjection<TProjection>, ISnapshot<TProjection>
{
serviceCollection.AddSingleton<ITransactionSubscriber>(serviceProvider =>
ProjectionSnapshotTransactionSubscriber<TProjection>.Create(serviceProvider, snapshotSessionOptionsName,
synchronousMode));
serviceCollection.Add<ITransactionSubscriber>
(
testMode ? ServiceLifetime.Singleton : ServiceLifetime.Scoped,
serviceProvider => ProjectionSnapshotTransactionSubscriber<TProjection>.Create(serviceProvider, snapshotSessionOptionsName,
testMode)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public EntitySnapshotTransactionSubscriber
(
ISnapshotRepositoryFactory<TEntity> snapshotRepositoryFactory,
string snapshotSessionOptionsName,
bool synchronousMode
) : base(synchronousMode)
bool testMode
) : base(testMode)
{
_snapshotRepositoryFactory = snapshotRepositoryFactory;
_snapshotSessionOptionsName = snapshotSessionOptionsName;
Expand Down Expand Up @@ -63,10 +63,10 @@ protected override async Task NotifyAsync(ITransaction transaction)
}

public static EntitySnapshotTransactionSubscriber<TEntity> Create(IServiceProvider serviceProvider,
string snapshotSessionOptionsName, bool synchronousMode)
string snapshotSessionOptionsName, bool testMode)
{
return ActivatorUtilities.CreateInstance<EntitySnapshotTransactionSubscriber<TEntity>>(serviceProvider,
snapshotSessionOptionsName,
synchronousMode);
testMode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public ProjectionSnapshotTransactionSubscriber
IProjectionStrategy<TProjection> projectionStrategy,
ISnapshotRepositoryFactory<TProjection> snapshotRepositoryFactory,
string snapshotSessionOptionsName,
bool synchronousMode
) : base(synchronousMode)
bool testMode
) : base(testMode)
{
_projectionStrategy = projectionStrategy;
_snapshotRepositoryFactory = snapshotRepositoryFactory;
Expand Down Expand Up @@ -77,10 +77,10 @@ protected override async Task NotifyAsync(ITransaction transaction)
}

public static ProjectionSnapshotTransactionSubscriber<TProjection> Create(IServiceProvider serviceProvider,
string snapshotSessionOptionsName, bool synchronousMode)
string snapshotSessionOptionsName, bool testMode)
{
return ActivatorUtilities.CreateInstance<ProjectionSnapshotTransactionSubscriber<TProjection>>(serviceProvider,
snapshotSessionOptionsName,
synchronousMode);
testMode);
}
}
10 changes: 5 additions & 5 deletions src/EntityDb.Common/Transactions/TransactionSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ namespace EntityDb.Common.Transactions;
/// </summary>
public abstract class TransactionSubscriber : ITransactionSubscriber
{
private readonly bool _synchronousMode;
private readonly bool _testMode;

/// <summary>
/// Constructs a new instance of <see cref="TransactionSubscriber" />.
/// </summary>
/// <param name="synchronousMode">If <c>true</c> then the task will be synchronously awaited before returning.</param>
protected TransactionSubscriber(bool synchronousMode)
/// <param name="testMode">If <c>true</c> then the task will be synchronously awaited before returning.</param>
protected TransactionSubscriber(bool testMode)
{
_synchronousMode = synchronousMode;
_testMode = testMode;
}

/// <inheritdoc cref="ITransactionSubscriber.Notify(ITransaction)" />
public void Notify(ITransaction transaction)
{
var task = Task.Run(async () => await NotifyAsync(transaction).ConfigureAwait(false));

if (_synchronousMode)
if (_testMode)
{
task.Wait();
}
Expand Down

0 comments on commit 12cc8da

Please sign in to comment.