@@ -23,15 +23,11 @@ public class ConnectionStringsOption : Dictionary<string, string> {}
2323public class ShardingConnectionsJsonFile : IShardingConnections
2424{
2525 private readonly ConnectionStringsOption _connectionDict ;
26+ private readonly IOptionsMonitor < ShardingSettingsOption > _shardingSettingsAccessor ;
2627 private readonly AuthPermissionsDbContext _context ;
2728 private readonly IDefaultLocalizer _localizeDefault ;
2829 private readonly AuthPermissionsOptions _options ;
2930
30- /// <summary>
31- /// This contains the most up to date data in the shardingsettings json file
32- /// </summary>
33- private List < DatabaseInformation > _databaseInformation ;
34-
3531 /// <summary>
3632 /// This contains the methods with are specific to a database provider
3733 /// </summary>
@@ -59,10 +55,9 @@ public ShardingConnectionsJsonFile(IOptionsSnapshot<ConnectionStringsOption> con
5955 {
6056 //thanks to https://stackoverflow.com/questions/37287427/get-multiple-connection-strings-in-appsettings-json-without-ef
6157 _connectionDict = connectionsAccessor . Value ;
58+ _shardingSettingsAccessor = shardingSettingsAccessor ;
6259 _context = context ;
6360 _options = options ;
64- SetDatabaseInformation ( shardingSettingsAccessor . CurrentValue ) ;
65- shardingSettingsAccessor . OnChange ( SetDatabaseInformation ) ;
6661
6762 DatabaseProviderMethods = databaseProviderMethods . ToDictionary ( x => x . AuthPDatabaseType ) ;
6863 ShardingDatabaseProviders = DatabaseProviderMethods . Values . ToDictionary ( x => x . DatabaseProviderShortName ) ;
@@ -78,7 +73,7 @@ public ShardingConnectionsJsonFile(IOptionsSnapshot<ConnectionStringsOption> con
7873 /// <returns>A list of <see cref="DatabaseInformation"/> from the sharding settings file</returns>
7974 public List < DatabaseInformation > GetAllPossibleShardingData ( )
8075 {
81- return _databaseInformation ;
76+ return GetDatabaseInformation ( ) ;
8277 }
8378
8479 /// <summary>
@@ -109,7 +104,7 @@ public IEnumerable<string> GetConnectionStringNames()
109104
110105 var result = new List < ( string databaseInfoName , bool ? hasOwnDb , List < string > ) > ( ) ;
111106 //Add sharding database names that have no tenants in them so that you can see all the connection string names
112- foreach ( var databaseInfoName in _databaseInformation . Select ( x => x . Name ) )
107+ foreach ( var databaseInfoName in GetDatabaseInformation ( ) . Select ( x => x . Name ) )
113108 {
114109 result . Add ( grouped . ContainsKey ( databaseInfoName )
115110 ? ( databaseInfoName ,
@@ -135,14 +130,14 @@ public string FormConnectionString(string databaseInfoName)
135130 if ( databaseInfoName == null )
136131 throw new AuthPermissionsException ( "The name of the database date can't be null" ) ;
137132
138- var databaseData = _databaseInformation . SingleOrDefault ( x => x . Name == databaseInfoName ) ;
133+ var databaseData = GetDatabaseInformation ( ) . SingleOrDefault ( x => x . Name == databaseInfoName ) ;
139134 if ( databaseData == null )
140135 throw new AuthPermissionsException (
141136 $ "The database information with the name of '{ databaseInfoName } ' wasn't founds.") ;
142137
143138 if ( ! _connectionDict . TryGetValue ( databaseData . ConnectionName , out var connectionString ) )
144139 throw new AuthPermissionsException (
145- $ "Could not find the connection name '{ connectionString } ' that the sharding database data '{ databaseInfoName } ' requires.") ;
140+ $ "Could not find the connection name '{ databaseData . ConnectionName } ' that the sharding database data '{ databaseInfoName } ' requires.") ;
146141
147142 if ( ! ShardingDatabaseProviders . TryGetValue ( databaseData . DatabaseType ,
148143 out IDatabaseSpecificMethods databaseSpecificMethods ) )
@@ -191,13 +186,12 @@ public IStatusGeneric TestFormingConnectionString(DatabaseInformation databaseIn
191186 //private methods
192187
193188 /// <summary>
194- /// This takes to information from the OptionsMonitor and sets the _databaseInformation parameter
189+ /// This gets the most up to date data in the shardingsettings json file
195190 /// </summary>
196- /// <returns></returns>
197- private void SetDatabaseInformation ( ShardingSettingsOption fromMonitor )
191+ private List < DatabaseInformation > GetDatabaseInformation ( )
198192 {
199- _databaseInformation = ( fromMonitor . ShardingDatabases == null || ! fromMonitor . ShardingDatabases . Any ( ) )
193+ return _shardingSettingsAccessor . CurrentValue == null || ! _shardingSettingsAccessor . CurrentValue . ShardingDatabases . Any ( )
200194 ? new List < DatabaseInformation > { DatabaseInformation . FormDefaultDatabaseInfo ( _options , _context ) }
201- : fromMonitor . ShardingDatabases ;
195+ : _shardingSettingsAccessor . CurrentValue . ShardingDatabases ;
202196 }
203197}
0 commit comments