77
88namespace ReactiveDomain . EventStore
99{
10+ /// <summary>
11+ /// A wrapper for EventStore Database (ESDB) connections.
12+ /// </summary>
1013 public class EventStoreConnectionWrapper : IStreamStoreConnection
1114 {
15+ /// <summary>
16+ /// The connection to the ESDB instance.
17+ /// </summary>
1218 public readonly ES . IEventStoreConnection EsConnection ;
19+ private readonly UserCredentials _credentials ;
1320 private bool _disposed ;
1421 private const int WriteBatchSize = 500 ;
1522
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 )
1729 {
1830 Ensure . NotNull ( eventStoreConnection , nameof ( eventStoreConnection ) ) ;
1931 EsConnection = eventStoreConnection ;
32+ _credentials = credentials ;
2033 }
2134
35+ /// <inheritdoc cref="IStreamStoreConnection"/>
2236 public string ConnectionName => EsConnection . ConnectionName ;
2337 private bool _connected ;
38+
39+ /// <inheritdoc cref="IStreamStoreConnection"/>
40+ /// <exception cref="CannotEstablishConnectionException">Thrown if a connection cannot be established to the ESDB.</exception>
2441 public void Connect ( )
2542 {
2643 if ( _connected ) { return ; }
@@ -36,12 +53,13 @@ public void Connect()
3653 }
3754 }
3855
56+ /// <inheritdoc cref="IStreamStoreConnection"/>
3957 public void Close ( )
4058 {
4159 EsConnection . Close ( ) ;
4260 }
4361
44-
62+ /// <inheritdoc cref="IStreamStoreConnection"/>
4563 public WriteResult AppendToStream (
4664 string stream ,
4765 long expectedVersion ,
@@ -52,7 +70,7 @@ public WriteResult AppendToStream(
5270 {
5371 if ( events . Length < WriteBatchSize )
5472 {
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 ( ) ;
5674 }
5775
5876 var transaction = EsConnection . StartTransactionAsync ( stream , expectedVersion ) . Result ;
@@ -77,13 +95,14 @@ public WriteResult AppendToStream(
7795
7896 }
7997
98+ /// <inheritdoc cref="IStreamStoreConnection"/>
8099 public StreamEventsSlice ReadStreamForward (
81100 string stream ,
82101 long start ,
83102 long count ,
84103 UserCredentials credentials = null )
85104 {
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 ;
87106 switch ( slice . Status )
88107 {
89108 case ES . SliceReadStatus . Success :
@@ -97,13 +116,14 @@ public StreamEventsSlice ReadStreamForward(
97116 }
98117 }
99118
119+ /// <inheritdoc cref="IStreamStoreConnection"/>
100120 public StreamEventsSlice ReadStreamBackward (
101121 string stream ,
102122 long start ,
103123 long count ,
104124 UserCredentials credentials = null )
105125 {
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 ;
107127 switch ( slice . Status )
108128 {
109129 case ES . SliceReadStatus . Success :
@@ -117,18 +137,19 @@ public StreamEventsSlice ReadStreamBackward(
117137 }
118138 }
119139
140+ /// <inheritdoc cref="IStreamStoreConnection"/>
120141 public IDisposable SubscribeToStream (
121142 string stream ,
122143 Action < RecordedEvent > eventAppeared ,
123144 Action < SubscriptionDropReason , Exception > subscriptionDropped = null ,
124- UserCredentials userCredentials = null )
145+ UserCredentials credentials = null )
125146 {
126147 var sub = EsConnection . SubscribeToStreamAsync (
127148 stream ,
128149 true ,
129150 async ( _ , evt ) => { eventAppeared ( evt . Event . ToRecordedEvent ( evt . OriginalEvent . EventNumber ) ) ; await Task . FromResult ( Unit . Default ) ; } ,
130151 ( _ , reason , ex ) => subscriptionDropped ? . Invoke ( ( SubscriptionDropReason ) ( int ) reason , ex ) ,
131- userCredentials ? . ToESCredentials ( ) ) . Result ;
152+ ( credentials ?? _credentials ) ? . ToESCredentials ( ) ) . Result ;
132153 return new Disposer ( ( ) =>
133154 {
134155 sub ? . Unsubscribe ( ) ;
@@ -137,14 +158,15 @@ public IDisposable SubscribeToStream(
137158 } ) ;
138159 }
139160
161+ /// <inheritdoc cref="IStreamStoreConnection"/>
140162 public IDisposable SubscribeToStreamFrom (
141163 string stream ,
142164 long ? lastCheckpoint ,
143165 CatchUpSubscriptionSettings settings ,
144166 Action < RecordedEvent > eventAppeared ,
145167 Action < Unit > liveProcessingStarted = null ,
146168 Action < SubscriptionDropReason , Exception > subscriptionDropped = null ,
147- UserCredentials userCredentials = null )
169+ UserCredentials credentials = null )
148170 {
149171 var sub = EsConnection . SubscribeToStreamFrom (
150172 stream ,
@@ -153,7 +175,7 @@ public IDisposable SubscribeToStreamFrom(
153175 async ( _ , evt ) => { eventAppeared ( evt . Event . ToRecordedEvent ( evt . OriginalEvent . EventNumber ) ) ; await Task . FromResult ( Unit . Default ) ; } ,
154176 _ => liveProcessingStarted ? . Invoke ( Unit . Default ) ,
155177 ( _ , reason , ex ) => subscriptionDropped ? . Invoke ( ( SubscriptionDropReason ) ( int ) reason , ex ) ,
156- userCredentials ? . ToESCredentials ( ) ) ;
178+ ( credentials ?? _credentials ) ? . ToESCredentials ( ) ) ;
157179
158180 return new Disposer ( ( ) =>
159181 {
@@ -162,10 +184,11 @@ public IDisposable SubscribeToStreamFrom(
162184 } ) ;
163185 }
164186
187+ /// <inheritdoc cref="IStreamStoreConnection"/>
165188 public IDisposable SubscribeToAll (
166189 Action < RecordedEvent > eventAppeared ,
167190 Action < SubscriptionDropReason , Exception > subscriptionDropped = null ,
168- UserCredentials userCredentials = null ,
191+ UserCredentials credentials = null ,
169192 bool resolveLinkTos = true )
170193 {
171194 var sub = EsConnection . SubscribeToAllAsync (
@@ -176,7 +199,7 @@ public IDisposable SubscribeToAll(
176199 await Task . FromResult ( Unit . Default ) ;
177200 } ,
178201 ( _ , reason , ex ) => subscriptionDropped ? . Invoke ( ( SubscriptionDropReason ) ( int ) reason , ex ) ,
179- userCredentials ? . ToESCredentials ( ) ) . Result ;
202+ ( credentials ?? _credentials ) ? . ToESCredentials ( ) ) . Result ;
180203 return new Disposer ( ( ) =>
181204 {
182205 sub ? . Unsubscribe ( ) ;
@@ -185,13 +208,14 @@ public IDisposable SubscribeToAll(
185208 } ) ;
186209 }
187210
211+ /// <inheritdoc cref="IStreamStoreConnection"/>
188212 public IDisposable SubscribeToAllFrom (
189213 Position from ,
190214 Action < RecordedEvent > eventAppeared ,
191215 CatchUpSubscriptionSettings settings = null ,
192216 Action liveProcessingStarted = null ,
193217 Action < SubscriptionDropReason , Exception > subscriptionDropped = null ,
194- UserCredentials userCredentials = null ,
218+ UserCredentials credentials = null ,
195219 bool resolveLinkTos = true )
196220 {
197221 var sub = EsConnection . SubscribeToAllFrom (
@@ -204,7 +228,7 @@ public IDisposable SubscribeToAllFrom(
204228 } ,
205229 __ => { liveProcessingStarted ? . Invoke ( ) ; } ,
206230 ( _ , reason , ex ) => subscriptionDropped ? . Invoke ( ( SubscriptionDropReason ) ( int ) reason , ex ) ,
207- userCredentials ? . ToESCredentials ( ) ) ;
231+ ( credentials ?? _credentials ) ? . ToESCredentials ( ) ) ;
208232 return new Disposer ( ( ) =>
209233 {
210234 sub . Stop ( TimeSpan . FromMilliseconds ( 250 ) ) ;
@@ -213,11 +237,13 @@ public IDisposable SubscribeToAllFrom(
213237 }
214238
215239
240+ /// <inheritdoc cref="IStreamStoreConnection"/>
216241 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 ( ) ;
218243
244+ /// <inheritdoc cref="IStreamStoreConnection"/>
219245 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 ( ) ;
221247
222248 public void Dispose ( )
223249 {
@@ -320,7 +346,7 @@ public static ReadDirection ToReadDirection(this ES.ReadDirection readDirection)
320346 case ES . ReadDirection . Backward :
321347 return ReadDirection . Backward ;
322348 default :
323- throw new ArgumentOutOfRangeException ( nameof ( readDirection ) , "Unknown ReadDirection returned from Eventstore " ) ;
349+ throw new ArgumentOutOfRangeException ( nameof ( readDirection ) , "Unknown ReadDirection returned from EventStore " ) ;
324350 }
325351 }
326352
0 commit comments