-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9c6da2
commit aac753c
Showing
10 changed files
with
157 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
|
||
namespace Eventuous { | ||
[PublicAPI] | ||
public static class AggregateStoreExtensions { | ||
public static Task<T> Load<T, TState, TId>( | ||
this IAggregateStore store, | ||
TId id, | ||
CancellationToken cancellationToken | ||
) | ||
where T : Aggregate<TState, TId>, new() | ||
where TState : AggregateState<TState, TId>, new() | ||
where TId : AggregateId | ||
=> store.Load<T>(id.ToString(), cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,30 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
|
||
namespace Eventuous | ||
{ | ||
/// <summary> | ||
/// Aggregate state persistent store | ||
/// </summary> | ||
[PublicAPI] | ||
public interface IAggregateStore { | ||
Task Store<T>(T entity, CancellationToken cancellationToken) where T : Aggregate; | ||
/// <summary> | ||
/// Store the new or updated aggregate state | ||
/// </summary> | ||
/// <param name="aggregate">Aggregate instance, which needs to be persisted</param> | ||
/// <param name="cancellationToken">Cancellation token</param> | ||
/// <typeparam name="T">Aggregate type</typeparam> | ||
/// <returns></returns> | ||
Task Store<T>(T aggregate, CancellationToken cancellationToken) where T : Aggregate; | ||
|
||
/// <summary> | ||
/// Load the aggregate from the store for a given id | ||
/// </summary> | ||
/// <param name="id">Aggregate id</param> | ||
/// <param name="cancellationToken">Cancellation token</param> | ||
/// <typeparam name="T">Aggregate type</typeparam> | ||
/// <returns></returns> | ||
Task<T> Load<T>(string id, CancellationToken cancellationToken) where T : Aggregate, new(); | ||
|
||
Task<T> LoadState<T, TId>(StreamName stream, CancellationToken cancellationToken) | ||
where T : AggregateState<T, TId>, new() where TId : AggregateId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
|
||
namespace Eventuous { | ||
/// <summary> | ||
/// State store allows loading an aggregate state from any stream. The idea is to be able to load a different | ||
/// version of the state than used in the aggregate itself, like an in-memory projection. | ||
/// </summary> | ||
[PublicAPI] | ||
public interface IStateStore { | ||
/// <summary> | ||
/// Load the aggregate state from the store, without initialising the aggregate itself | ||
/// </summary> | ||
/// <param name="stream">Aggregate event</param> | ||
/// <param name="cancellationToken"></param> | ||
/// <typeparam name="T"></typeparam> | ||
/// <typeparam name="TId"></typeparam> | ||
/// <returns></returns> | ||
Task<T> LoadState<T, TId>(StreamName stream, CancellationToken cancellationToken) | ||
where T : AggregateState<T, TId>, new() where TId : AggregateId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
|
||
namespace Eventuous { | ||
[PublicAPI] | ||
public class StateStore : IStateStore { | ||
readonly IEventStore _eventStore; | ||
readonly IEventSerializer _serializer; | ||
|
||
public StateStore(IEventStore eventStore, IEventSerializer serializer) { | ||
_eventStore = Ensure.NotNull(eventStore, nameof(eventStore)); | ||
_serializer = Ensure.NotNull(serializer, nameof(serializer)); | ||
} | ||
|
||
public async Task<T> LoadState<T, TId>(StreamName stream, CancellationToken cancellationToken) | ||
where T : AggregateState<T, TId>, new() where TId : AggregateId { | ||
var state = new T(); | ||
|
||
await _eventStore.ReadStream(stream, StreamReadPosition.Start, Fold, cancellationToken).Ignore(); | ||
|
||
return state; | ||
|
||
void Fold(StreamEvent streamEvent) { | ||
var evt = Deserialize(streamEvent); | ||
if (evt == null) return; | ||
|
||
state = state.When(evt); | ||
} | ||
|
||
object? Deserialize(StreamEvent streamEvent) | ||
=> _serializer.Deserialize(streamEvent.Data.AsSpan(), streamEvent.EventType); | ||
} | ||
} | ||
} |
Oops, something went wrong.