| | | 1 | | namespace ZhEaIsNsAaBn.APPSettings; |
| | | 2 | | |
| | | 3 | | /// <summary>Default service: writes through the registered writer, then reloads the database sources.</summary> |
| | | 4 | | internal sealed class EFConfigurationService< TSettingsTable, TDbContext > : IEFConfigurationService<TSettingsTable, TDb |
| | | 5 | | where TSettingsTable : class, IEFConfigurationValue, new() |
| | | 6 | | where TDbContext : DbContext |
| | | 7 | | { |
| | | 8 | | private readonly IConfiguration _configuration; |
| | | 9 | | private readonly IEFConfigurationWriter<TSettingsTable, TDbContext> _writer; |
| | | 10 | | |
| | | 11 | | |
| | 24 | 12 | | public EFConfigurationService( IConfiguration configuration, IEFConfigurationWriter<TSettingsTable, TDbContext> writ |
| | | 13 | | { |
| | 24 | 14 | | _configuration = configuration; |
| | 24 | 15 | | _writer = writer; |
| | 24 | 16 | | } |
| | | 17 | | |
| | | 18 | | |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | public async Task InsertAsync( string key, string value, CancellationToken cancellationToken = default ) |
| | | 21 | | { |
| | 12 | 22 | | await _writer.InsertAsync( key, value, cancellationToken ).ConfigureAwait( false ); |
| | | 23 | | |
| | 12 | 24 | | ManualReload(); |
| | 12 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public async Task InsertFromJsonAsync( string json, CancellationToken cancellationToken = default ) |
| | | 29 | | { |
| | 4 | 30 | | await _writer.InsertFromJsonAsync( json, cancellationToken ).ConfigureAwait( false ); |
| | | 31 | | |
| | 4 | 32 | | ManualReload(); |
| | 4 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | public void ManualReload() |
| | | 37 | | { |
| | 24 | 38 | | if ( _configuration is not IConfigurationRoot root ) |
| | | 39 | | { |
| | 0 | 40 | | throw new InvalidOperationException( "The registered IConfiguration is not an IConfigurationRoot, so it can' |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | |
| | 24 | 44 | | var reloaded = false; |
| | | 45 | | |
| | 88 | 46 | | foreach ( var provider in root.Providers.OfType<EFConfigurationProvider<TSettingsTable, TDbContext>>() ) |
| | | 47 | | { |
| | 20 | 48 | | provider.Reload(); |
| | 20 | 49 | | reloaded = true; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | |
| | 24 | 53 | | if ( !reloaded ) |
| | | 54 | | { |
| | 4 | 55 | | throw new InvalidOperationException( $"The registered IConfiguration has no {typeof( TSettingsTable ).Name} |
| | | 56 | | } |
| | 20 | 57 | | } |
| | | 58 | | } |