@@ -6,59 +6,67 @@ namespace Config.SqlStreamStore
66{
77 public class StreamStoreConfigurationSource : IConfigurationSource
88 {
9- private readonly string _connectionStringKey ;
10- private readonly Func < IConfigRepository > _getConfigRepository ;
11- private Func < string , IStreamStore > _streamStoreFactory ;
9+ private readonly BuildStreamStoreFromConfig _buildStreamStoreFromConfig ;
1210
13- public StreamStoreConfigurationSource ( string connectionStringKey , Func < string , IStreamStore > streamStoreFactory )
11+ public delegate IStreamStore BuildStreamStoreFromConnectionString ( string connectionString ) ;
12+ public delegate IStreamStore BuildStreamStoreFromConfig ( IConfigurationRoot configurationRoot ) ;
13+
14+ public delegate IConfigRepository BuildConfigRepository ( ) ;
15+
16+ private readonly BuildConfigRepository _getConfigRepository ;
17+
18+ public StreamStoreConfigurationSource ( BuildStreamStoreFromConfig buildStreamStoreFromConfig )
1419 {
15- _connectionStringKey = connectionStringKey ;
16- _streamStoreFactory = streamStoreFactory ;
20+ _buildStreamStoreFromConfig = buildStreamStoreFromConfig ;
1721 }
1822
19- public StreamStoreConfigurationSource ( Func < IStreamStore > getStreamStore ) :
20- this ( ( ) =>
23+ public StreamStoreConfigurationSource ( string connectionStringKey , BuildStreamStoreFromConnectionString streamStoreFactory )
24+ {
25+ _buildStreamStoreFromConfig = ( c =>
2126 {
22- var streamStore = getStreamStore ( ) ;
23- if ( streamStore == null ) return null ;
27+ var connectionString = c [ connectionStringKey ] ;
28+
29+ if ( string . IsNullOrEmpty ( connectionString ) )
30+ {
31+ throw new InvalidOperationException (
32+ $ "Cannot create SqlStreamStore repository, becuase connection string (key: '{ connectionStringKey } ') has not been configured.") ;
33+ }
34+
35+ return streamStoreFactory ( connectionString ) ;
36+ } ) ;
37+ }
2438
25- return new ConfigRepository ( streamStore ) ;
26- } )
39+ public StreamStoreConfigurationSource ( Func < IStreamStore > getStreamStore ) :
40+ this ( ( ) => new ConfigRepository ( getStreamStore ( ) ) )
2741 {
2842
2943 }
3044
31- public StreamStoreConfigurationSource ( Func < IConfigRepository > getConfigRepository )
45+ public StreamStoreConfigurationSource ( BuildConfigRepository getConfigRepository )
3246 {
3347 _getConfigRepository = getConfigRepository ;
3448 }
3549
3650 public IConfigurationProvider Build ( IConfigurationBuilder builder )
3751 {
38- if ( _getConfigRepository = = null )
52+ if ( _getConfigRepository ! = null )
3953 {
40- var innerBuilder = new ConfigurationBuilder ( ) ;
41- foreach ( var source in builder . Sources )
42- {
43- if ( source != this )
44- {
45- innerBuilder . Add ( source ) ;
46- }
47- }
48-
49- var connectionString = innerBuilder . Build ( ) [ _connectionStringKey ] ;
54+ return new StreamStoreConfigurationProvider ( _getConfigRepository ( ) ) ;
55+ }
5056
51- if ( string . IsNullOrEmpty ( connectionString ) )
57+ var innerBuilder = new ConfigurationBuilder ( ) ;
58+ foreach ( var source in builder . Sources )
59+ {
60+ if ( source != this )
5261 {
53- throw new InvalidOperationException ( $ "Cannot create SqlStreamStore repository, becuase connection string (key: ' { _connectionStringKey } ') has not been configured." ) ;
62+ innerBuilder . Add ( source ) ;
5463 }
55-
56- var repo = new ConfigRepository ( _streamStoreFactory ( connectionString ) ) ;
57-
58- return new StreamStoreConfigurationProvider ( ( ) => repo ) ;
5964 }
6065
61- return new StreamStoreConfigurationProvider ( _getConfigRepository ) ;
66+ var repo = new ConfigRepository ( _buildStreamStoreFromConfig ( innerBuilder . Build ( ) ) ) ;
67+
68+ return new StreamStoreConfigurationProvider ( repo ) ;
69+
6270 }
6371
6472
0 commit comments