Skip to content

Commit ce18119

Browse files
committed
Snapshot cleanup
1 parent 4def0d9 commit ce18119

12 files changed

Lines changed: 51 additions & 107 deletions

File tree

Source/EventFlow.TestHelpers/Aggregates/ThingyAggregate.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
using EventFlow.Aggregates;
3030
using EventFlow.Exceptions;
3131
using EventFlow.Snapshots;
32+
using EventFlow.Snapshots.Strategies;
3233
using EventFlow.TestHelpers.Aggregates.Entities;
3334
using EventFlow.TestHelpers.Aggregates.Events;
3435
using EventFlow.TestHelpers.Aggregates.Snapshots;
@@ -47,7 +48,7 @@ public class ThingyAggregate : SnapshotAggregateRoot<ThingyAggregate, ThingyId,
4748
public IReadOnlyCollection<PingId> PingsReceived => _pingsReceived;
4849
public IReadOnlyCollection<ThingyMessage> Messages => _messages;
4950

50-
public ThingyAggregate(ThingyId id) : base(id)
51+
public ThingyAggregate(ThingyId id) : base(id, new SnapshotStrategy())
5152
{
5253
Register<ThingyPingEvent>(e => _pingsReceived.Add(e.PingId));
5354
Register<ThingyMessageAddedEvent>(e => _messages.Add(e.ThingyMessage));

Source/EventFlow.Tests/EventFlow.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<Compile Include="UnitTests\Core\ReflectionHelperTests.cs" />
7575
<Compile Include="UnitTests\Core\RetryDelayTests.cs" />
7676
<Compile Include="UnitTests\Core\VersionedTypes\VersionedTypeDefinitionServiceTestSuite.cs" />
77-
<Compile Include="UnitTests\EventStores\Snapshots\SnapshotBuilderTests.cs" />
77+
<Compile Include="UnitTests\EventStores\Snapshots\SnapshotSerilizerTests.cs" />
7878
<Compile Include="UnitTests\EventStores\Snapshots\SnapshotUpgradeServiceTests.cs" />
7979
<Compile Include="UnitTests\Extensions\AggregatesExtensionsTests.cs" />
8080
<Compile Include="UnitTests\Extensions\CommandHandlerExtensionsTests.cs" />

Source/EventFlow.Tests/UnitTests/EventStores/Snapshots/SnapshotBuilderTests.cs renamed to Source/EventFlow.Tests/UnitTests/EventStores/Snapshots/SnapshotSerilizerTests.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,15 @@
2222
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2323
//
2424

25-
using System.Threading;
26-
using System.Threading.Tasks;
2725
using NUnit.Framework;
2826
using EventFlow.Logs;
2927
using EventFlow.Snapshots;
3028
using EventFlow.TestHelpers;
31-
using EventFlow.TestHelpers.Aggregates;
3229
using EventFlow.TestHelpers.Aggregates.Snapshots;
33-
using FluentAssertions;
3430

3531
namespace EventFlow.Tests.UnitTests.EventStores.Snapshots
3632
{
37-
public class SnapshotBuilderTests : TestsFor<SnapshotBuilder>
33+
public class SnapshotSerilizerTests : TestsFor<SnapshotSerilizer>
3834
{
3935
[SetUp]
4036
public void SetUp()
@@ -43,16 +39,5 @@ public void SetUp()
4339
snapshotDefinitionService.Load(typeof(ThingySnapshotV1), typeof(ThingySnapshotV2), typeof(ThingySnapshot));
4440
Inject<ISnapshotDefinitionService>(snapshotDefinitionService);
4541
}
46-
47-
[Test]
48-
public async Task BuildSnapshotAsync()
49-
{
50-
// Act
51-
var serializedSnapshot = await Sut.BuildSnapshotAsync(new ThingyAggregate(ThingyId.New), CancellationToken.None);
52-
53-
// Assert
54-
serializedSnapshot.Metadata.SnapshotName.Should().Be("thingy");
55-
serializedSnapshot.Metadata.SnapshotVersion.Should().Be(3);
56-
}
5742
}
5843
}

Source/EventFlow/Aggregates/SnapshotAggregateRoot.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using EventFlow.EventStores;
3131
using EventFlow.Extensions;
3232
using EventFlow.Snapshots;
33+
using EventFlow.Snapshots.Strategies;
3334

3435
namespace EventFlow.Aggregates
3536
{
@@ -39,10 +40,14 @@ public abstract class SnapshotAggregateRoot<TAggregate, TIdentity, TSnapshot> :
3940
where TIdentity : IIdentity
4041
where TSnapshot : ISnapshot
4142
{
43+
protected ISnapshotStrategy SnapshotStrategy { get; }
44+
4245
protected SnapshotAggregateRoot(
43-
TIdentity id)
46+
TIdentity id,
47+
ISnapshotStrategy snapshotStrategy)
4448
: base(id)
4549
{
50+
SnapshotStrategy = snapshotStrategy;
4651
}
4752

4853
public int? SnapshotVersion { get; private set; }
@@ -73,13 +78,22 @@ public override async Task LoadAsync(
7378
ApplyEvents(domainEvents);
7479
}
7580

76-
public override Task<IReadOnlyCollection<IDomainEvent>> CommitAsync(
81+
public override async Task<IReadOnlyCollection<IDomainEvent>> CommitAsync(
7782
IEventStore eventStore,
7883
ISnapshotStore snapshotStore,
7984
ISourceId sourceId,
8085
CancellationToken cancellationToken)
8186
{
82-
return base.CommitAsync(eventStore, snapshotStore, sourceId, cancellationToken);
87+
var domainEvents = await CommitAsync(eventStore, snapshotStore, sourceId, cancellationToken).ConfigureAwait(false);
88+
89+
if (!await SnapshotStrategy.ShouldCreateSnapshotAsync(this, cancellationToken).ConfigureAwait(false))
90+
{
91+
return domainEvents;
92+
}
93+
94+
95+
96+
return domainEvents;
8397
}
8498

8599
public async Task<SnapshotContainer> CreateSnapshotContainerAsync(CancellationToken cancellationToken)

Source/EventFlow/EventFlow.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,13 @@
151151
<Compile Include="EventStores\IEventUpgrader.cs" />
152152
<Compile Include="Snapshots\Stores\InMemory\InMemorySnapshotPersistence.cs" />
153153
<Compile Include="Snapshots\CommittedSnapshot.cs" />
154-
<Compile Include="Snapshots\ISnapshotBuilder.cs" />
154+
<Compile Include="Snapshots\ISnapshotSerilizer.cs" />
155155
<Compile Include="Snapshots\ISnapshotMetadata.cs" />
156156
<Compile Include="Snapshots\Stores\ISnapshotPersistence.cs" />
157157
<Compile Include="Snapshots\ISnapshotUpgradeService.cs" />
158158
<Compile Include="Snapshots\SerializedSnapshot.cs" />
159159
<Compile Include="Snapshots\SnapshotContainer.cs" />
160-
<Compile Include="Snapshots\SnapshotBuilder.cs" />
160+
<Compile Include="Snapshots\SnapshotSerilizer.cs" />
161161
<Compile Include="Snapshots\SnapshotMetadata.cs" />
162162
<Compile Include="Snapshots\SnapshotMetadataKeys.cs" />
163163
<Compile Include="Snapshots\Stores\Null\NullSnapshotPersistence.cs" />

Source/EventFlow/EventFlowOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private void RegisterDefaults(IServiceRegistration serviceRegistration)
176176
serviceRegistration.Register<ICommandBus, CommandBus>();
177177
serviceRegistration.Register<IAggregateStore, AggregateStore>();
178178
serviceRegistration.Register<ISnapshotStore, SnapshotStore>();
179-
serviceRegistration.Register<ISnapshotBuilder, SnapshotBuilder>();
179+
serviceRegistration.Register<ISnapshotSerilizer, SnapshotSerilizer>();
180180
serviceRegistration.Register<ISnapshotStrategy, SnapshotStrategy>();
181181
serviceRegistration.Register<ISnapshotPersistence, InMemorySnapshotPersistence>(Lifetime.Singleton);
182182
serviceRegistration.Register<ISnapshotUpgradeService, SnapshotUpgradeService>();

Source/EventFlow/Snapshots/ISnapshotMetadata.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ public interface ISnapshotMetadata : IMetadataContainer
3131
string SnapshotName { get; }
3232
int SnapshotVersion { get; }
3333
int AggregateSequenceNumber { get; }
34+
string AggregateId { get; }
35+
string AggregateName { get; }
3436
}
3537
}

Source/EventFlow/Snapshots/ISnapshotBuilder.cs renamed to Source/EventFlow/Snapshots/ISnapshotSerilizer.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@
2929

3030
namespace EventFlow.Snapshots
3131
{
32-
public interface ISnapshotBuilder
32+
public interface ISnapshotSerilizer
3333
{
34-
Task<SerializedSnapshot> BuildSnapshotAsync(IAggregateRoot aggregateRoot, CancellationToken cancellationToken);
35-
36-
Task<SerializedSnapshot> BuildSnapshotAsync<TAggregate, TIdentity, TSnapshot>(
34+
Task<SerializedSnapshot> SerilizeAsync<TAggregate, TIdentity, TSnapshot>(
3735
SnapshotContainer snapshotContainer,
3836
CancellationToken cancellationToken)
3937
where TAggregate : ISnapshotAggregateRoot<TIdentity, TSnapshot>

Source/EventFlow/Snapshots/ISnapshotStore.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ Task<SnapshotContainer> LoadSnapshotAsync<TAggregate, TIdentity, TSnapshot>(
3939
where TSnapshot : ISnapshot;
4040

4141
Task StoreSnapshotAsync<TAggregate, TIdentity, TSnapshot>(
42-
TAggregate aggregate,
42+
TIdentity identity,
43+
SnapshotContainer snapshotContainer,
4344
CancellationToken cancellationToken)
4445
where TAggregate : ISnapshotAggregateRoot<TIdentity, TSnapshot>
4546
where TIdentity : IIdentity

Source/EventFlow/Snapshots/SnapshotMetadata.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public SnapshotMetadata(params KeyValuePair<string, string>[] keyValuePairs)
4949
{
5050
}
5151

52+
public string AggregateId => GetMetadataValue(SnapshotMetadataKeys.AggregateId);
53+
public string AggregateName => GetMetadataValue(SnapshotMetadataKeys.AggregateName);
5254
public int AggregateSequenceNumber => GetMetadataValue(SnapshotMetadataKeys.AggregateSequenceNumber, int.Parse);
5355
public string SnapshotName => GetMetadataValue(SnapshotMetadataKeys.SnapshotName);
5456
public int SnapshotVersion => GetMetadataValue(SnapshotMetadataKeys.SnapshotVersion, int.Parse);

0 commit comments

Comments
 (0)