Skip to content

Commit

Permalink
Merge pull request #48 from jacqueskang/rc
Browse files Browse the repository at this point in the history
refactor: update AggregateRepository to use snapshot by default
  • Loading branch information
jacqueskang authored Feb 5, 2020
2 parents 2eeb425 + df1cbfa commit d0ab17f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion doc/Snapshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ To enable this feature you need to implement the following:

1. By default when calling `AggregateRepository<TAggregate, TKey>.FindAggregateAsync()` it automatically retrieve the last snapshot. You can force not loading any snapshot by calling:
```csharp
FindAggregateAsync(aggregateId, useSnapshot: false)
FindAggregateAsync(aggregateId, ignoreSnapshot: true)
```
16 changes: 15 additions & 1 deletion src/EventSourcing.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject
..\.travis.yml = ..\.travis.yml
Directory.Build.props = Directory.Build.props
..\README.md = ..\README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JKang.EventSourcing.Persistence.EfCore", "JKang.EventSourcing.Persistence.EfCore\JKang.EventSourcing.Persistence.EfCore.csproj", "{5473865D-3737-4719-893B-9DD6A10E5A35}"
Expand All @@ -29,7 +30,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JKang.EventSourcing.Persist
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JKang.EventSourcing.Options", "JKang.EventSourcing.Options\JKang.EventSourcing.Options.csproj", "{5230D7B5-EE3C-43B4-BCBB-2FA6487BDA7D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JKang.EventSourcing.Abstractions.Tests", "JKang.EventSourcing.Abstractions.Tests\JKang.EventSourcing.Abstractions.Tests.csproj", "{1D8A6FFC-1D14-4EA0-A42A-959E38D46EC1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JKang.EventSourcing.Abstractions.Tests", "JKang.EventSourcing.Abstractions.Tests\JKang.EventSourcing.Abstractions.Tests.csproj", "{1D8A6FFC-1D14-4EA0-A42A-959E38D46EC1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "doc", "doc", "{6374E9CF-DFD8-4A86-BBEC-6F2D2E09B8D6}"
ProjectSection(SolutionItems) = preProject
..\doc\CosmosDBSetup.md = ..\doc\CosmosDBSetup.md
..\doc\DynamoDBSetup.md = ..\doc\DynamoDBSetup.md
..\doc\EfCoreSetup.md = ..\doc\EfCoreSetup.md
..\doc\FileSystemSetup.md = ..\doc\FileSystemSetup.md
..\doc\Snapshots.md = ..\doc\Snapshots.md
..\doc\StoreInitialization.md = ..\doc\StoreInitialization.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -85,6 +96,9 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{6374E9CF-DFD8-4A86-BBEC-6F2D2E09B8D6} = {3CE8F82A-51A6-46BC-9A78-590642AA5242}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E4CE6D6B-D967-402C-BDBE-0949B55C1F1B}
EndGlobalSection
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using JKang.EventSourcing.Domain;
using JKang.EventSourcing.Events;
using JKang.EventSourcing.Snapshotting;
using JKang.EventSourcing.Snapshotting.Persistence;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -59,11 +58,12 @@ protected Task<TKey[]> GetAggregateIdsAsync()
return _eventStore.GetAggregateIdsAsync();
}

protected async Task<TAggregate> FindAggregateAsync(TKey id, bool useSnapshot,
protected async Task<TAggregate> FindAggregateAsync(TKey id,
bool ignoreSnapshot = false,
CancellationToken cancellationToken = default)
{
IAggregateSnapshot<TKey> snapshot = null;
if (useSnapshot)
if (!ignoreSnapshot)
{
snapshot = await _snapshotStore
.FindLastSnapshotAsync(id, cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public GiftCardRepository(

public Task SaveGiftCardAsync(GiftCard giftCard) => SaveAggregateAsync(giftCard);

public Task<GiftCard> FindGiftCardAsync(Guid id, bool useSnapshot = true)
=> FindAggregateAsync(id, useSnapshot);
public Task<GiftCard> FindGiftCardAsync(Guid id) => FindAggregateAsync(id);

public Task<Guid[]> GetGiftCardIdsAsync() => GetAggregateIdsAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace JKang.EventSourcing.TestingFixtures
public interface IGiftCardRepository
{
Task SaveGiftCardAsync(GiftCard giftCard);
Task<GiftCard> FindGiftCardAsync(Guid id, bool useSnapshot = true);
Task<GiftCard> FindGiftCardAsync(Guid id);
Task<Guid[]> GetGiftCardIdsAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public DetailsModel(IGiftCardRepository repository)

public async Task<IActionResult> OnGetAsync(Guid id)
{
GiftCard = await _repository.FindGiftCardAsync(id, true)
GiftCard = await _repository.FindGiftCardAsync(id)
?? throw new InvalidOperationException("Gift card not found");

return Page();
}

public async Task<IActionResult> OnPostDebitAsync(Guid id)
{
GiftCard = await _repository.FindGiftCardAsync(id, true)
GiftCard = await _repository.FindGiftCardAsync(id)
?? throw new InvalidOperationException("Gift card not found");

GiftCard.Debit(Amount);
Expand Down

0 comments on commit d0ab17f

Please sign in to comment.