Skip to content

Commit f34bae2

Browse files
feature: get at version
allows you to request the entity at a specific version
1 parent 7f9ecd5 commit f34bae2

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/EntityDb.Abstractions/Entities/IEntityRepository.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ public interface IEntityRepository<TEntity> : IDisposable, IAsyncDisposable
2424
/// <returns>The current state of a <typeparamref name="TEntity" /> or constructs a new <typeparamref name="TEntity" />.</returns>
2525
Task<TEntity> GetCurrent(Guid entityId);
2626

27+
/// <summary>
28+
/// Returns a previous state of <typeparamref name="TEntity" />.
29+
/// </summary>
30+
/// <param name="entityId">The id of the entity.</param>
31+
/// <param name="lteVersionNumber">The version of the entity to fetch.</param>
32+
/// <returns>A previous state of <typeparamref name="TEntity" />.</returns>
33+
Task<TEntity> GetAtVersion(Guid entityId, ulong lteVersionNumber);
34+
2735
/// <summary>
2836
/// Inserts a single transaction with an atomic commit.
2937
/// </summary>

src/EntityDb.Common/Entities/EntityRepository.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ public async Task<TEntity> GetCurrent(Guid entityId)
8383
return entity;
8484
}
8585

86+
public async Task<TEntity> GetAtVersion(Guid entityId, ulong lteVersionNumber)
87+
{
88+
var commandQuery = new GetEntityAtVersionQuery(entityId, lteVersionNumber);
89+
90+
var commands = await _transactionRepository.GetCommands(commandQuery);
91+
92+
return _constructingStrategy
93+
.Construct(entityId)
94+
.Reduce(commands);
95+
}
96+
8697
public async Task<bool> PutTransaction(ITransaction<TEntity> transaction)
8798
{
8899
var success = await _transactionRepository.PutTransaction(transaction);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using EntityDb.Abstractions.Queries;
2+
using EntityDb.Abstractions.Queries.FilterBuilders;
3+
using EntityDb.Abstractions.Queries.SortBuilders;
4+
using System;
5+
6+
namespace EntityDb.Common.Queries
7+
{
8+
internal sealed record GetEntityAtVersionQuery(Guid EntityId, ulong LteVersionNumber) : ICommandQuery
9+
{
10+
public TFilter GetFilter<TFilter>(ICommandFilterBuilder<TFilter> builder)
11+
{
12+
return builder.And
13+
(
14+
builder.EntityIdIn(EntityId),
15+
builder.EntityVersionNumberLte(LteVersionNumber)
16+
);
17+
}
18+
19+
public TSort GetSort<TSort>(ICommandSortBuilder<TSort> builder)
20+
{
21+
return builder.Combine
22+
(
23+
builder.EntityVersionNumber(true)
24+
);
25+
}
26+
27+
public int? Skip => null;
28+
29+
public int? Take => null;
30+
}
31+
}

0 commit comments

Comments
 (0)