|
| 1 | +using EventStore.Client; |
| 2 | +using Kurrent.Client.Core.Serialization; |
| 3 | +using Kurrent.Client.Streams.GettingState; |
| 4 | + |
| 5 | +namespace Kurrent.Client.Streams.DecisionMaking; |
| 6 | + |
| 7 | +public interface IAggregateStore<TAggregate, TEvent> |
| 8 | + where TAggregate : IAggregate<TEvent> { |
| 9 | + Task<StateAtPointInTime<TAggregate>> Get( |
| 10 | + string streamName, |
| 11 | + CancellationToken ct = default |
| 12 | + ); |
| 13 | + |
| 14 | + Task<IWriteResult> AddAsync( |
| 15 | + string streamName, |
| 16 | + TAggregate aggregate, |
| 17 | + AppendToStreamOptions? appendToStreamOptions, |
| 18 | + CancellationToken ct = default |
| 19 | + ); |
| 20 | + |
| 21 | + Task<IWriteResult> UpdateAsync( |
| 22 | + string streamName, |
| 23 | + TAggregate aggregate, |
| 24 | + AppendToStreamOptions? appendToStreamOptions, |
| 25 | + CancellationToken ct = default |
| 26 | + ); |
| 27 | + |
| 28 | + Task<IWriteResult> HandleAsync( |
| 29 | + string streamName, |
| 30 | + Func<TAggregate, CancellationToken, ValueTask> handle, |
| 31 | + DecideOptions<TAggregate>? decideOption, |
| 32 | + CancellationToken ct = default |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +public static class AggregateStoreExtensions { |
| 37 | + public static Task<IWriteResult> AddAsync<TAggregate, TEvent>( |
| 38 | + IAggregateStore<TAggregate, TEvent> aggregateStore, |
| 39 | + string streamName, |
| 40 | + TAggregate aggregate, |
| 41 | + CancellationToken ct = default |
| 42 | + ) where TAggregate : IAggregate<TEvent> => |
| 43 | + aggregateStore.AddAsync( |
| 44 | + streamName, |
| 45 | + aggregate, |
| 46 | + new AppendToStreamOptions { ExpectedStreamState = StreamState.NoStream }, |
| 47 | + ct |
| 48 | + ); |
| 49 | + |
| 50 | + public static Task<IWriteResult> HandleAsync<TAggregate, TEvent>( |
| 51 | + IAggregateStore<TAggregate, TEvent> aggregateStore, |
| 52 | + string streamName, |
| 53 | + Func<TAggregate, CancellationToken, ValueTask> handle, |
| 54 | + CancellationToken ct = default |
| 55 | + ) where TAggregate : IAggregate<TEvent> => |
| 56 | + aggregateStore.HandleAsync( |
| 57 | + streamName, |
| 58 | + handle, |
| 59 | + new DecideOptions<TAggregate>(), |
| 60 | + ct |
| 61 | + ); |
| 62 | + |
| 63 | + public static Task<IWriteResult> HandleAsync<TAggregate, TEvent>( |
| 64 | + IAggregateStore<TAggregate, TEvent> aggregateStore, |
| 65 | + string streamName, |
| 66 | + Action<TAggregate> handle, |
| 67 | + CancellationToken ct = default |
| 68 | + ) where TAggregate : IAggregate<TEvent> => |
| 69 | + aggregateStore.HandleAsync( |
| 70 | + streamName, |
| 71 | + (state, _) => { |
| 72 | + handle(state); |
| 73 | + return new ValueTask(); |
| 74 | + }, |
| 75 | + new DecideOptions<TAggregate>(), |
| 76 | + ct |
| 77 | + ); |
| 78 | + |
| 79 | + public static Task<IWriteResult> HandleAsync<TAggregate, TEvent>( |
| 80 | + IAggregateStore<TAggregate, TEvent> aggregateStore, |
| 81 | + string streamName, |
| 82 | + Action<TAggregate> handle, |
| 83 | + DecideOptions<TAggregate>? decideOption, |
| 84 | + CancellationToken ct = default |
| 85 | + ) where TAggregate : IAggregate<TEvent> => |
| 86 | + aggregateStore.HandleAsync( |
| 87 | + streamName, |
| 88 | + (state, _) => { |
| 89 | + handle(state); |
| 90 | + return new ValueTask(); |
| 91 | + }, |
| 92 | + decideOption, |
| 93 | + ct |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +public class AggregateStore<TAggregate, TEvent>(KurrentClient client, AggregateStoreOptions<TAggregate> options) |
| 98 | + : IAggregateStore<TAggregate, TEvent> |
| 99 | + where TAggregate : IAggregate<TEvent> |
| 100 | + where TEvent : notnull { |
| 101 | + public Task<StateAtPointInTime<TAggregate>> Get(string streamName, CancellationToken ct = default) => |
| 102 | + client.GetStateAsync(streamName, options.StateBuilder, ct); |
| 103 | + |
| 104 | + public Task<IWriteResult> AddAsync( |
| 105 | + string streamName, |
| 106 | + TAggregate aggregate, |
| 107 | + AppendToStreamOptions? appendToStreamOptions, |
| 108 | + CancellationToken ct = default |
| 109 | + ) { |
| 110 | + appendToStreamOptions ??= new AppendToStreamOptions(); |
| 111 | + |
| 112 | + if (appendToStreamOptions.ExpectedStreamState == null && appendToStreamOptions.ExpectedStreamRevision == null) |
| 113 | + appendToStreamOptions.ExpectedStreamState = StreamState.NoStream; |
| 114 | + |
| 115 | + return client.AppendToStreamAsync(streamName, aggregate.DequeueUncommittedMessages(), appendToStreamOptions, ct); |
| 116 | + } |
| 117 | + |
| 118 | + public Task<IWriteResult> UpdateAsync( |
| 119 | + string streamName, |
| 120 | + TAggregate aggregate, |
| 121 | + AppendToStreamOptions? appendToStreamOptions, |
| 122 | + CancellationToken ct = default |
| 123 | + ) { |
| 124 | + appendToStreamOptions ??= new AppendToStreamOptions(); |
| 125 | + |
| 126 | + if (appendToStreamOptions.ExpectedStreamState == null && appendToStreamOptions.ExpectedStreamRevision == null) |
| 127 | + appendToStreamOptions.ExpectedStreamState = StreamState.StreamExists; |
| 128 | + |
| 129 | + return client.AppendToStreamAsync(streamName, aggregate.DequeueUncommittedMessages(), appendToStreamOptions, ct); |
| 130 | + } |
| 131 | + |
| 132 | + public Task<IWriteResult> HandleAsync( |
| 133 | + string streamName, |
| 134 | + Func<TAggregate, CancellationToken, ValueTask> handle, |
| 135 | + DecideOptions<TAggregate>? decideOption, |
| 136 | + CancellationToken ct = default |
| 137 | + ) => |
| 138 | + client.DecideAsync( |
| 139 | + streamName, |
| 140 | + async (state, token) => { |
| 141 | + await handle(state, token); |
| 142 | + return state.DequeueUncommittedMessages(); |
| 143 | + }, |
| 144 | + options.StateBuilder, |
| 145 | + decideOption ?? options.DecideOptions, |
| 146 | + ct |
| 147 | + ); |
| 148 | +} |
0 commit comments