Skip to content

Commit b6c5790

Browse files
refactor: move putting snapshots from Get to Put
this is the final piece for allowing snapshot-only mode :) it also makes all of the sense in the world
1 parent e53f66e commit b6c5790

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

EntityDb.Abstractions/Transactions/ITransactionCommand.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ namespace EntityDb.Abstractions.Transactions
1010
/// <typeparam name="TEntity">The type of entity to be modified.</typeparam>
1111
public interface ITransactionCommand<TEntity>
1212
{
13+
/// <summary>
14+
/// A snapshot of the entity before the command.
15+
/// </summary>
16+
TEntity? PreviousSnapshot { get; }
17+
18+
/// <summary>
19+
/// A snapshot of the entity after the command.
20+
/// </summary>
21+
TEntity NextSnapshot { get; }
22+
1323
/// <summary>
1424
/// The id of the entity.
1525
/// </summary>

EntityDb.Common.Tests/Transactions/TransactionTestsBase.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ public async Task GivenReadOnlyMode_WhenPuttingTransaction_ThenThrow()
406406
{
407407
new TransactionCommand<TransactionEntity>
408408
(
409+
default,
410+
default!,
409411
Guid.NewGuid(),
410412
0,
411413
new DoNothing(),
@@ -441,6 +443,8 @@ static ITransaction<TransactionEntity> NewTransaction(Guid transactionId)
441443
{
442444
new TransactionCommand<TransactionEntity>
443445
(
446+
default,
447+
default!,
444448
Guid.NewGuid(),
445449
0,
446450
new DoNothing(),
@@ -482,6 +486,8 @@ public async Task GivenNonUniqueVersionNumbers_WhenInsertingCommands_ThenReturnF
482486
{
483487
new TransactionCommand<TransactionEntity>
484488
(
489+
default,
490+
default!,
485491
entityId,
486492
previousVersionNumber,
487493
new DoNothing(),
@@ -491,6 +497,8 @@ public async Task GivenNonUniqueVersionNumbers_WhenInsertingCommands_ThenReturnF
491497
),
492498
new TransactionCommand<TransactionEntity>
493499
(
500+
default,
501+
default!,
494502
entityId,
495503
previousVersionNumber,
496504
new DoNothing(),
@@ -532,6 +540,8 @@ static ITransaction<TransactionEntity> NewTransaction(Guid entityId, ulong previ
532540
{
533541
new TransactionCommand<TransactionEntity>
534542
(
543+
default,
544+
default!,
535545
entityId,
536546
previousVersionNumber,
537547
new DoNothing(),
@@ -581,6 +591,8 @@ public async Task GivenNonUniqueSubversionNumbers_WhenInsertingFacts_ThenReturnF
581591
{
582592
new TransactionCommand<TransactionEntity>
583593
(
594+
default,
595+
default!,
584596
entityId,
585597
0,
586598
new DoNothing(),
@@ -630,6 +642,8 @@ public async Task GivenNonUniqueLeases_WhenInsertingLeaseDocuments_ThenReturnFal
630642
{
631643
new TransactionCommand<TransactionEntity>
632644
(
645+
default,
646+
default!,
633647
Guid.NewGuid(),
634648
0,
635649
new DoNothing(),
@@ -642,6 +656,8 @@ public async Task GivenNonUniqueLeases_WhenInsertingLeaseDocuments_ThenReturnFal
642656
),
643657
new TransactionCommand<TransactionEntity>
644658
(
659+
default,
660+
default!,
645661
Guid.NewGuid(),
646662
0,
647663
new DoNothing(),

EntityDb.Common/Entities/EntityRepository.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using EntityDb.Common.Queries;
66
using System;
77
using System.Diagnostics.CodeAnalysis;
8+
using System.Linq;
89
using System.Threading.Tasks;
910

1011
namespace EntityDb.Common.Entities
@@ -49,16 +50,26 @@ public async Task<TEntity> Get(Guid entityId)
4950

5051
entity = entity.Reduce(facts);
5152

52-
if (_serviceProvider.ShouldCache(snapshot, entity) && _snapshotRepository != null)
53-
{
54-
await _snapshotRepository.PutSnapshot(entityId, entity);
55-
}
56-
5753
return entity;
5854
}
5955

6056
public Task<bool> Put(ITransaction<TEntity> transaction)
6157
{
58+
if (_snapshotRepository != null)
59+
{
60+
var lastCommands = transaction.Commands
61+
.GroupBy(command => command.EntityId)
62+
.Select(group => group.Last());
63+
64+
foreach (var lastCommand in lastCommands)
65+
{
66+
if (_serviceProvider.ShouldCache(lastCommand.PreviousSnapshot, lastCommand.NextSnapshot))
67+
{
68+
_snapshotRepository.PutSnapshot(lastCommand.EntityId, lastCommand.NextSnapshot);
69+
}
70+
}
71+
}
72+
6273
return _transactionRepository.PutTransaction(transaction);
6374
}
6475

EntityDb.Common/Transactions/TransactionBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ private void AddTransactionCommand(Guid entityId, ICommand<TEntity> command)
6060

6161
_transactionCommands.Add(new TransactionCommand<TEntity>
6262
(
63+
previousEntity,
64+
nextEntity,
6365
entityId,
6466
previousVersionNumber,
6567
command,

EntityDb.Common/Transactions/TransactionCommand.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@
55

66
namespace EntityDb.Common.Transactions
77
{
8-
internal sealed record TransactionCommand<TEntity>(Guid EntityId, ulong ExpectedPreviousVersionNumber, ICommand<TEntity> Command, ITransactionFact<TEntity>[] Facts, ILease[] DeleteLeases, ILease[] InsertLeases) : ITransactionCommand<TEntity>
8+
internal sealed record TransactionCommand<TEntity>
9+
(
10+
TEntity? PreviousSnapshot,
11+
TEntity NextSnapshot,
12+
Guid EntityId,
13+
ulong ExpectedPreviousVersionNumber,
14+
ICommand<TEntity> Command,
15+
ITransactionFact<TEntity>[] Facts,
16+
ILease[] DeleteLeases,
17+
ILease[] InsertLeases
18+
) : ITransactionCommand<TEntity>
919
{
1020
}
1121
}

0 commit comments

Comments
 (0)