-
-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathEventStoreDBExtensions.cs
More file actions
187 lines (161 loc) · 5.27 KB
/
EventStoreDBExtensions.cs
File metadata and controls
187 lines (161 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using Core.EventStoreDB.Serialization;
using Core.Exceptions;
using Core.OpenTelemetry;
using Core.Reflection;
using EventStore.Client;
namespace Core.EventStoreDB.Events;
public static class EventStoreDBExtensions
{
public static async Task<TEntity?> Find<TEntity>(
this EventStoreClient eventStore,
Func<TEntity, object, TEntity> when,
string id,
CancellationToken cancellationToken
) where TEntity: class
{
var readResult = eventStore.ReadStreamAsync(
Direction.Forwards,
id,
StreamPosition.Start,
cancellationToken: cancellationToken
);
if (await readResult.ReadState.ConfigureAwait(false) == ReadState.StreamNotFound)
return null;
var result = ObjectFactory<TEntity>.GetDefaultOrUninitialized();
await foreach (var @event in readResult)
result = when(result, @event);
return result;
}
public static async Task<TEntity> Find<TEntity>(
this EventStoreClient eventStore,
Func<TEntity> getDefault,
Func<TEntity, object, TEntity> when,
string id,
CancellationToken cancellationToken)
{
var readResult = eventStore.ReadStreamAsync(
Direction.Forwards,
id,
StreamPosition.Start,
cancellationToken: cancellationToken
);
if (await readResult.ReadState.ConfigureAwait(false) == ReadState.StreamNotFound)
throw AggregateNotFoundException.For<TEntity>(id);
var result = getDefault();
await foreach (var resolvedEvent in readResult)
result = when(result, resolvedEvent.Deserialize()!);
return result;
}
public static async Task<List<object>> ReadStream(
this EventStoreClient eventStore,
string id,
CancellationToken cancellationToken)
{
var readResult = eventStore.ReadStreamAsync(
Direction.Forwards,
id,
StreamPosition.Start,
cancellationToken: cancellationToken
);
if (await readResult.ReadState.ConfigureAwait(false) == ReadState.StreamNotFound)
return [];
var result = new List<object>();
await foreach (var @event in readResult)
result.Add(@event.Deserialize()!);
return result;
}
public static async Task<ulong> Append(
this EventStoreClient eventStore,
string id,
object @event,
CancellationToken cancellationToken
)
{
var result = await eventStore.AppendToStreamAsync(
id,
StreamState.Any,
[@event.ToJsonEventData(TelemetryPropagator.GetPropagationContext())],
cancellationToken: cancellationToken
).ConfigureAwait(false);
return result.NextExpectedStreamRevision;
}
public static async Task<ulong> Append(
this EventStoreClient eventStore,
string id,
object @event,
ulong expectedRevision,
CancellationToken cancellationToken
)
{
var result = await eventStore.AppendToStreamAsync(
id,
expectedRevision,
[@event.ToJsonEventData(TelemetryPropagator.GetPropagationContext())],
cancellationToken: cancellationToken
).ConfigureAwait(false);
return result.NextExpectedStreamRevision;
}
public static async Task<TEvent?> ReadLastEvent<TEvent>(
this EventStoreClient eventStore,
string id,
CancellationToken ct
) where TEvent : class
{
var resolvedEvent = await eventStore.ReadLastEvent(id, ct).ConfigureAwait(false);
return resolvedEvent?.Deserialize<TEvent>();
}
public static async Task<ResolvedEvent?> ReadLastEvent(
this EventStoreClient eventStore,
string id,
CancellationToken ct
)
{
var result = eventStore.ReadStreamAsync(
Direction.Backwards,
id,
StreamPosition.End,
maxCount: 1,
cancellationToken: ct
);
if (await result.ReadState.ConfigureAwait(false) == ReadState.StreamNotFound)
{
return null;
}
await foreach (var @event in result)
return @event;
return null;
}
public static async Task AppendToStreamWithSingleEvent(
this EventStoreClient eventStore,
string id,
object @event,
CancellationToken ct
)
{
var eventData = new[] { @event.ToJsonEventData() };
var result = await eventStore.AppendToStreamAsync(
id,
StreamState.StreamExists,
eventData,
options =>
{
options.ThrowOnAppendFailure = false;
},
cancellationToken: ct
).ConfigureAwait(false);
if (result is SuccessResult)
return;
await eventStore.SetStreamMetadataAsync(
id,
StreamState.NoStream,
new StreamMetadata(maxCount: 1),
cancellationToken: ct
).ConfigureAwait(false);
await eventStore.AppendToStreamAsync(
id,
StreamState.NoStream,
eventData,
cancellationToken: ct
).ConfigureAwait(false);
}
}