7
7
8
8
namespace ReactiveDomain . EventStore
9
9
{
10
+ /// <summary>
11
+ /// A wrapper for EventStore Database (ESDB) connections.
12
+ /// </summary>
10
13
public class EventStoreConnectionWrapper : IStreamStoreConnection
11
14
{
15
+ /// <summary>
16
+ /// The connection to the ESDB instance.
17
+ /// </summary>
12
18
public readonly ES . IEventStoreConnection EsConnection ;
19
+ private readonly UserCredentials _credentials ;
13
20
private bool _disposed ;
14
21
private const int WriteBatchSize = 500 ;
15
22
16
- public EventStoreConnectionWrapper ( ES . IEventStoreConnection eventStoreConnection )
23
+ /// <summary>
24
+ /// Creates a wrapper around an ESDB connection.
25
+ /// </summary>
26
+ /// <param name="eventStoreConnection">A connection to an EventStoreDB instance.</param>
27
+ /// <param name="credentials">The optional credentials to use when connecting.</param>
28
+ public EventStoreConnectionWrapper ( ES . IEventStoreConnection eventStoreConnection , UserCredentials credentials = null )
17
29
{
18
30
Ensure . NotNull ( eventStoreConnection , nameof ( eventStoreConnection ) ) ;
19
31
EsConnection = eventStoreConnection ;
32
+ _credentials = credentials ;
20
33
}
21
34
35
+ /// <inheritdoc cref="IStreamStoreConnection"/>
22
36
public string ConnectionName => EsConnection . ConnectionName ;
23
37
private bool _connected ;
38
+
39
+ /// <inheritdoc cref="IStreamStoreConnection"/>
40
+ /// <exception cref="CannotEstablishConnectionException">Thrown if a connection cannot be established to the ESDB.</exception>
24
41
public void Connect ( )
25
42
{
26
43
if ( _connected ) { return ; }
@@ -36,12 +53,13 @@ public void Connect()
36
53
}
37
54
}
38
55
56
+ /// <inheritdoc cref="IStreamStoreConnection"/>
39
57
public void Close ( )
40
58
{
41
59
EsConnection . Close ( ) ;
42
60
}
43
61
44
-
62
+ /// <inheritdoc cref="IStreamStoreConnection"/>
45
63
public WriteResult AppendToStream (
46
64
string stream ,
47
65
long expectedVersion ,
@@ -52,7 +70,7 @@ public WriteResult AppendToStream(
52
70
{
53
71
if ( events . Length < WriteBatchSize )
54
72
{
55
- return EsConnection . AppendToStreamAsync ( stream , expectedVersion , events . ToESEventData ( ) , credentials . ToESCredentials ( ) ) . Result . ToWriteResult ( ) ;
73
+ return EsConnection . AppendToStreamAsync ( stream , expectedVersion , events . ToESEventData ( ) , ( credentials ?? _credentials ) ? . ToESCredentials ( ) ) . Result . ToWriteResult ( ) ;
56
74
}
57
75
58
76
var transaction = EsConnection . StartTransactionAsync ( stream , expectedVersion ) . Result ;
@@ -77,13 +95,14 @@ public WriteResult AppendToStream(
77
95
78
96
}
79
97
98
+ /// <inheritdoc cref="IStreamStoreConnection"/>
80
99
public StreamEventsSlice ReadStreamForward (
81
100
string stream ,
82
101
long start ,
83
102
long count ,
84
103
UserCredentials credentials = null )
85
104
{
86
- var slice = EsConnection . ReadStreamEventsForwardAsync ( stream , start , ( int ) count , true , credentials . ToESCredentials ( ) ) . Result ;
105
+ var slice = EsConnection . ReadStreamEventsForwardAsync ( stream , start , ( int ) count , true , ( credentials ?? _credentials ) ? . ToESCredentials ( ) ) . Result ;
87
106
switch ( slice . Status )
88
107
{
89
108
case ES . SliceReadStatus . Success :
@@ -97,13 +116,14 @@ public StreamEventsSlice ReadStreamForward(
97
116
}
98
117
}
99
118
119
+ /// <inheritdoc cref="IStreamStoreConnection"/>
100
120
public StreamEventsSlice ReadStreamBackward (
101
121
string stream ,
102
122
long start ,
103
123
long count ,
104
124
UserCredentials credentials = null )
105
125
{
106
- var slice = EsConnection . ReadStreamEventsBackwardAsync ( stream , start , ( int ) count , true , credentials . ToESCredentials ( ) ) . Result ;
126
+ var slice = EsConnection . ReadStreamEventsBackwardAsync ( stream , start , ( int ) count , true , ( credentials ?? _credentials ) ? . ToESCredentials ( ) ) . Result ;
107
127
switch ( slice . Status )
108
128
{
109
129
case ES . SliceReadStatus . Success :
@@ -117,18 +137,19 @@ public StreamEventsSlice ReadStreamBackward(
117
137
}
118
138
}
119
139
140
+ /// <inheritdoc cref="IStreamStoreConnection"/>
120
141
public IDisposable SubscribeToStream (
121
142
string stream ,
122
143
Action < RecordedEvent > eventAppeared ,
123
144
Action < SubscriptionDropReason , Exception > subscriptionDropped = null ,
124
- UserCredentials userCredentials = null )
145
+ UserCredentials credentials = null )
125
146
{
126
147
var sub = EsConnection . SubscribeToStreamAsync (
127
148
stream ,
128
149
true ,
129
150
async ( _ , evt ) => { eventAppeared ( evt . Event . ToRecordedEvent ( evt . OriginalEvent . EventNumber ) ) ; await Task . FromResult ( Unit . Default ) ; } ,
130
151
( _ , reason , ex ) => subscriptionDropped ? . Invoke ( ( SubscriptionDropReason ) ( int ) reason , ex ) ,
131
- userCredentials ? . ToESCredentials ( ) ) . Result ;
152
+ ( credentials ?? _credentials ) ? . ToESCredentials ( ) ) . Result ;
132
153
return new Disposer ( ( ) =>
133
154
{
134
155
sub ? . Unsubscribe ( ) ;
@@ -137,14 +158,15 @@ public IDisposable SubscribeToStream(
137
158
} ) ;
138
159
}
139
160
161
+ /// <inheritdoc cref="IStreamStoreConnection"/>
140
162
public IDisposable SubscribeToStreamFrom (
141
163
string stream ,
142
164
long ? lastCheckpoint ,
143
165
CatchUpSubscriptionSettings settings ,
144
166
Action < RecordedEvent > eventAppeared ,
145
167
Action < Unit > liveProcessingStarted = null ,
146
168
Action < SubscriptionDropReason , Exception > subscriptionDropped = null ,
147
- UserCredentials userCredentials = null )
169
+ UserCredentials credentials = null )
148
170
{
149
171
var sub = EsConnection . SubscribeToStreamFrom (
150
172
stream ,
@@ -153,7 +175,7 @@ public IDisposable SubscribeToStreamFrom(
153
175
async ( _ , evt ) => { eventAppeared ( evt . Event . ToRecordedEvent ( evt . OriginalEvent . EventNumber ) ) ; await Task . FromResult ( Unit . Default ) ; } ,
154
176
_ => liveProcessingStarted ? . Invoke ( Unit . Default ) ,
155
177
( _ , reason , ex ) => subscriptionDropped ? . Invoke ( ( SubscriptionDropReason ) ( int ) reason , ex ) ,
156
- userCredentials ? . ToESCredentials ( ) ) ;
178
+ ( credentials ?? _credentials ) ? . ToESCredentials ( ) ) ;
157
179
158
180
return new Disposer ( ( ) =>
159
181
{
@@ -162,10 +184,11 @@ public IDisposable SubscribeToStreamFrom(
162
184
} ) ;
163
185
}
164
186
187
+ /// <inheritdoc cref="IStreamStoreConnection"/>
165
188
public IDisposable SubscribeToAll (
166
189
Action < RecordedEvent > eventAppeared ,
167
190
Action < SubscriptionDropReason , Exception > subscriptionDropped = null ,
168
- UserCredentials userCredentials = null ,
191
+ UserCredentials credentials = null ,
169
192
bool resolveLinkTos = true )
170
193
{
171
194
var sub = EsConnection . SubscribeToAllAsync (
@@ -176,7 +199,7 @@ public IDisposable SubscribeToAll(
176
199
await Task . FromResult ( Unit . Default ) ;
177
200
} ,
178
201
( _ , reason , ex ) => subscriptionDropped ? . Invoke ( ( SubscriptionDropReason ) ( int ) reason , ex ) ,
179
- userCredentials ? . ToESCredentials ( ) ) . Result ;
202
+ ( credentials ?? _credentials ) ? . ToESCredentials ( ) ) . Result ;
180
203
return new Disposer ( ( ) =>
181
204
{
182
205
sub ? . Unsubscribe ( ) ;
@@ -185,13 +208,14 @@ public IDisposable SubscribeToAll(
185
208
} ) ;
186
209
}
187
210
211
+ /// <inheritdoc cref="IStreamStoreConnection"/>
188
212
public IDisposable SubscribeToAllFrom (
189
213
Position from ,
190
214
Action < RecordedEvent > eventAppeared ,
191
215
CatchUpSubscriptionSettings settings = null ,
192
216
Action liveProcessingStarted = null ,
193
217
Action < SubscriptionDropReason , Exception > subscriptionDropped = null ,
194
- UserCredentials userCredentials = null ,
218
+ UserCredentials credentials = null ,
195
219
bool resolveLinkTos = true )
196
220
{
197
221
var sub = EsConnection . SubscribeToAllFrom (
@@ -204,7 +228,7 @@ public IDisposable SubscribeToAllFrom(
204
228
} ,
205
229
__ => { liveProcessingStarted ? . Invoke ( ) ; } ,
206
230
( _ , reason , ex ) => subscriptionDropped ? . Invoke ( ( SubscriptionDropReason ) ( int ) reason , ex ) ,
207
- userCredentials ? . ToESCredentials ( ) ) ;
231
+ ( credentials ?? _credentials ) ? . ToESCredentials ( ) ) ;
208
232
return new Disposer ( ( ) =>
209
233
{
210
234
sub . Stop ( TimeSpan . FromMilliseconds ( 250 ) ) ;
@@ -213,11 +237,13 @@ public IDisposable SubscribeToAllFrom(
213
237
}
214
238
215
239
240
+ /// <inheritdoc cref="IStreamStoreConnection"/>
216
241
public void DeleteStream ( string stream , long expectedVersion , UserCredentials credentials = null )
217
- => EsConnection . DeleteStreamAsync ( stream , expectedVersion , credentials . ToESCredentials ( ) ) . Wait ( ) ;
242
+ => EsConnection . DeleteStreamAsync ( stream , expectedVersion , ( credentials ?? _credentials ) . ToESCredentials ( ) ) . Wait ( ) ;
218
243
244
+ /// <inheritdoc cref="IStreamStoreConnection"/>
219
245
public void HardDeleteStream ( string stream , long expectedVersion , UserCredentials credentials = null )
220
- => EsConnection . DeleteStreamAsync ( stream , expectedVersion , true , credentials . ToESCredentials ( ) ) . Wait ( ) ;
246
+ => EsConnection . DeleteStreamAsync ( stream , expectedVersion , true , ( credentials ?? _credentials ) . ToESCredentials ( ) ) . Wait ( ) ;
221
247
222
248
public void Dispose ( )
223
249
{
@@ -320,7 +346,7 @@ public static ReadDirection ToReadDirection(this ES.ReadDirection readDirection)
320
346
case ES . ReadDirection . Backward :
321
347
return ReadDirection . Backward ;
322
348
default :
323
- throw new ArgumentOutOfRangeException ( nameof ( readDirection ) , "Unknown ReadDirection returned from Eventstore " ) ;
349
+ throw new ArgumentOutOfRangeException ( nameof ( readDirection ) , "Unknown ReadDirection returned from EventStore " ) ;
324
350
}
325
351
}
326
352
0 commit comments