| | | 1 | | namespace ZhEaIsNsAaBn.APPSettings; |
| | | 2 | | |
| | | 3 | | /// <summary>Configuration provider that reads key/value rows from a database table through EF Core.</summary> |
| | | 4 | | /// <typeparam name="TSettingsTable">The settings entity.</typeparam> |
| | | 5 | | /// <typeparam name="TDbContext">The context that holds the settings table.</typeparam> |
| | | 6 | | public sealed class EFConfigurationProvider< TSettingsTable, TDbContext > : ConfigurationProvider |
| | | 7 | | where TSettingsTable : class, IEFConfigurationValue, new() |
| | | 8 | | where TDbContext : DbContext |
| | | 9 | | { |
| | | 10 | | private readonly EFConfigurationProviderSettings<TSettingsTable, TDbContext> _settings; |
| | | 11 | | |
| | | 12 | | |
| | | 13 | | /// <summary>Creates the provider.</summary> |
| | | 14 | | /// <param name="settings">The provider settings.</param> |
| | | 15 | | /// <exception cref="ArgumentNullException"><paramref name="settings"/> is <see langword="null"/>.</exception> |
| | 44 | 16 | | public EFConfigurationProvider( EFConfigurationProviderSettings<TSettingsTable, TDbContext> settings ) |
| | 44 | 17 | | => _settings = settings ?? throw new ArgumentNullException( nameof( settings ) ); |
| | | 18 | | |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Reads the rows matching the query (no tracking). Keys are case-insensitive; when two rows share a key, the last |
| | | 22 | | /// </summary> |
| | | 23 | | /// <exception cref="InvalidOperationException">No <see cref="IDbContextFactory{TContext}"/> is set.</exception> |
| | | 24 | | public override void Load() |
| | | 25 | | { |
| | 64 | 26 | | using var context = _settings.GetRequiredDbContextFactory().CreateDbContext(); |
| | | 27 | | |
| | 64 | 28 | | var data = new Dictionary<string, string?>( StringComparer.OrdinalIgnoreCase ); |
| | | 29 | | |
| | | 30 | | |
| | 320 | 31 | | foreach ( var row in context.Set<TSettingsTable>().AsNoTracking().Where( _settings.Query ) ) { data[ row.Key ] = |
| | | 32 | | |
| | | 33 | | |
| | 64 | 34 | | Data = data; |
| | 128 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary>Reads the table again and signals the change, so reload tokens and <c>IOptionsMonitor</c> see the new v |
| | | 38 | | /// <exception cref="InvalidOperationException">No <see cref="IDbContextFactory{TContext}"/> is set.</exception> |
| | | 39 | | public void Reload() |
| | | 40 | | { |
| | 20 | 41 | | Load(); |
| | 20 | 42 | | OnReload(); |
| | 20 | 43 | | } |
| | | 44 | | } |