Skip to content

Commit 12cc8da

Browse files
bugfix: transaction subscribers need the same service lifetime as repository factories
1 parent f4f12d7 commit 12cc8da

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed

src/EntityDb.Common/Extensions/ServiceCollectionExtensions.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,18 @@ public static void AddEntity<TEntity>(this IServiceCollection serviceCollection)
8989
/// </summary>
9090
/// <param name="serviceCollection">The service collection.</param>
9191
/// <param name="snapshotSessionOptionsName">The agent's intent for the snapshot repository.</param>
92-
/// <param name="synchronousMode">If <c>true</c> then snapshots will be synchronously recorded.</param>
92+
/// <param name="testMode">If <c>true</c> then snapshots will be synchronously recorded.</param>
9393
/// <typeparam name="TEntity">The type of the entity.</typeparam>
9494
public static void AddEntitySnapshotTransactionSubscriber<TEntity>(this IServiceCollection serviceCollection,
95-
string snapshotSessionOptionsName, bool synchronousMode = false)
95+
string snapshotSessionOptionsName, bool testMode = false)
9696
where TEntity : IEntity<TEntity>, ISnapshot<TEntity>
9797
{
98-
serviceCollection.AddSingleton<ITransactionSubscriber>(serviceProvider =>
99-
EntitySnapshotTransactionSubscriber<TEntity>.Create(serviceProvider, snapshotSessionOptionsName,
100-
synchronousMode));
98+
serviceCollection.Add<ITransactionSubscriber>
99+
(
100+
testMode ? ServiceLifetime.Singleton : ServiceLifetime.Scoped,
101+
serviceProvider => EntitySnapshotTransactionSubscriber<TEntity>.Create(serviceProvider, snapshotSessionOptionsName,
102+
testMode)
103+
);
101104
}
102105

103106
/// <summary>
@@ -121,15 +124,18 @@ public static void AddProjection<TProjection, TProjectionStrategy>(
121124
/// </summary>
122125
/// <param name="serviceCollection">The service collection.</param>
123126
/// <param name="snapshotSessionOptionsName">The agent's intent for the snapshot repository.</param>
124-
/// <param name="synchronousMode">If <c>true</c> then snapshots will be synchronously recorded.</param>
127+
/// <param name="testMode">If <c>true</c> then snapshots will be synchronously recorded.</param>
125128
/// <typeparam name="TProjection">The type of the projection.</typeparam>
126129
public static void AddProjectionSnapshotTransactionSubscriber<TProjection>(
127130
this IServiceCollection serviceCollection,
128-
string snapshotSessionOptionsName, bool synchronousMode = false)
131+
string snapshotSessionOptionsName, bool testMode = false)
129132
where TProjection : IProjection<TProjection>, ISnapshot<TProjection>
130133
{
131-
serviceCollection.AddSingleton<ITransactionSubscriber>(serviceProvider =>
132-
ProjectionSnapshotTransactionSubscriber<TProjection>.Create(serviceProvider, snapshotSessionOptionsName,
133-
synchronousMode));
134+
serviceCollection.Add<ITransactionSubscriber>
135+
(
136+
testMode ? ServiceLifetime.Singleton : ServiceLifetime.Scoped,
137+
serviceProvider => ProjectionSnapshotTransactionSubscriber<TProjection>.Create(serviceProvider, snapshotSessionOptionsName,
138+
testMode)
139+
);
134140
}
135141
}

src/EntityDb.Common/Transactions/EntitySnapshotTransactionSubscriber.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public EntitySnapshotTransactionSubscriber
2020
(
2121
ISnapshotRepositoryFactory<TEntity> snapshotRepositoryFactory,
2222
string snapshotSessionOptionsName,
23-
bool synchronousMode
24-
) : base(synchronousMode)
23+
bool testMode
24+
) : base(testMode)
2525
{
2626
_snapshotRepositoryFactory = snapshotRepositoryFactory;
2727
_snapshotSessionOptionsName = snapshotSessionOptionsName;
@@ -63,10 +63,10 @@ protected override async Task NotifyAsync(ITransaction transaction)
6363
}
6464

6565
public static EntitySnapshotTransactionSubscriber<TEntity> Create(IServiceProvider serviceProvider,
66-
string snapshotSessionOptionsName, bool synchronousMode)
66+
string snapshotSessionOptionsName, bool testMode)
6767
{
6868
return ActivatorUtilities.CreateInstance<EntitySnapshotTransactionSubscriber<TEntity>>(serviceProvider,
6969
snapshotSessionOptionsName,
70-
synchronousMode);
70+
testMode);
7171
}
7272
}

src/EntityDb.Common/Transactions/ProjectionSnapshotTransactionSubscriber.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public ProjectionSnapshotTransactionSubscriber
2626
IProjectionStrategy<TProjection> projectionStrategy,
2727
ISnapshotRepositoryFactory<TProjection> snapshotRepositoryFactory,
2828
string snapshotSessionOptionsName,
29-
bool synchronousMode
30-
) : base(synchronousMode)
29+
bool testMode
30+
) : base(testMode)
3131
{
3232
_projectionStrategy = projectionStrategy;
3333
_snapshotRepositoryFactory = snapshotRepositoryFactory;
@@ -77,10 +77,10 @@ protected override async Task NotifyAsync(ITransaction transaction)
7777
}
7878

7979
public static ProjectionSnapshotTransactionSubscriber<TProjection> Create(IServiceProvider serviceProvider,
80-
string snapshotSessionOptionsName, bool synchronousMode)
80+
string snapshotSessionOptionsName, bool testMode)
8181
{
8282
return ActivatorUtilities.CreateInstance<ProjectionSnapshotTransactionSubscriber<TProjection>>(serviceProvider,
8383
snapshotSessionOptionsName,
84-
synchronousMode);
84+
testMode);
8585
}
8686
}

src/EntityDb.Common/Transactions/TransactionSubscriber.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ namespace EntityDb.Common.Transactions;
88
/// </summary>
99
public abstract class TransactionSubscriber : ITransactionSubscriber
1010
{
11-
private readonly bool _synchronousMode;
11+
private readonly bool _testMode;
1212

1313
/// <summary>
1414
/// Constructs a new instance of <see cref="TransactionSubscriber" />.
1515
/// </summary>
16-
/// <param name="synchronousMode">If <c>true</c> then the task will be synchronously awaited before returning.</param>
17-
protected TransactionSubscriber(bool synchronousMode)
16+
/// <param name="testMode">If <c>true</c> then the task will be synchronously awaited before returning.</param>
17+
protected TransactionSubscriber(bool testMode)
1818
{
19-
_synchronousMode = synchronousMode;
19+
_testMode = testMode;
2020
}
2121

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

27-
if (_synchronousMode)
27+
if (_testMode)
2828
{
2929
task.Wait();
3030
}

0 commit comments

Comments
 (0)