| | | 1 | | namespace ZhEaIsNsAaBn.APPSettings.Builders; |
| | | 2 | | |
| | | 3 | | /// <summary>Records options; <see cref="Build"/> adds the source and registers the services.</summary> |
| | | 4 | | internal sealed class EFConfigurationBuilder< TSettingsTable, TDbContext > : IEFConfigurationBuilder<TSettingsTable, TDb |
| | | 5 | | where TSettingsTable : class, IEFConfigurationValue, new() |
| | | 6 | | where TDbContext : DbContext |
| | | 7 | | { |
| | | 8 | | private readonly IConfigurationBuilder _configuration; |
| | | 9 | | private readonly IServiceProvider? _serviceProvider; |
| | 56 | 10 | | private readonly EFConfigurationProviderSettings<TSettingsTable, TDbContext> _settings = new(); |
| | | 11 | | private Type? _writerType; |
| | | 12 | | |
| | | 13 | | |
| | 56 | 14 | | public EFConfigurationBuilder( IConfigurationBuilder configuration, IServiceProvider? serviceProvider ) |
| | | 15 | | { |
| | 56 | 16 | | _configuration = configuration; |
| | 56 | 17 | | _serviceProvider = serviceProvider; |
| | 56 | 18 | | } |
| | | 19 | | |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public IConfigurationBuilder Build( IServiceCollection services ) |
| | | 23 | | { |
| | 52 | 24 | | ArgumentNullException.ThrowIfNull( services ); |
| | | 25 | | |
| | | 26 | | |
| | 52 | 27 | | _settings.DbContextFactory ??= _serviceProvider?.GetService<IDbContextFactory<TDbContext>>(); |
| | 52 | 28 | | _settings.GetRequiredDbContextFactory(); |
| | | 29 | | |
| | | 30 | | |
| | | 31 | | // TODO@ZhEaIsNsAaBn: automatic reload on SaveChanges — old version never fired; rebuild on the change-tracker |
| | 52 | 32 | | if ( _writerType is null && _settings.ReloadOnChange ) { WithReloader(); } |
| | | 33 | | |
| | | 34 | | |
| | 76 | 35 | | if ( _writerType is not null ) { RegisterReloader( services, _writerType ); } |
| | | 36 | | |
| | | 37 | | |
| | 48 | 38 | | _configuration.Add( new EFConfigurationSource<TSettingsTable, TDbContext>( _settings ) ); |
| | | 39 | | |
| | 48 | 40 | | return _configuration; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public IEFConfigurationBuilder<TSettingsTable, TDbContext> WithDbContextFactory( IDbContextFactory<TDbContext> facto |
| | | 45 | | { |
| | 44 | 46 | | ArgumentNullException.ThrowIfNull( factory ); |
| | | 47 | | |
| | 44 | 48 | | _settings.DbContextFactory = factory; |
| | | 49 | | |
| | 44 | 50 | | return this; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | | 54 | | public IEFConfigurationBuilder<TSettingsTable, TDbContext> WithQuery( Expression<Func<TSettingsTable, bool>> query ) |
| | | 55 | | { |
| | 4 | 56 | | ArgumentNullException.ThrowIfNull( query ); |
| | | 57 | | |
| | 4 | 58 | | _settings.Query = query; |
| | | 59 | | |
| | 4 | 60 | | return this; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | | 64 | | public IEFConfigurationBuilder<TSettingsTable, TDbContext> WithReloadDelay( int delay ) |
| | | 65 | | { |
| | 4 | 66 | | ArgumentOutOfRangeException.ThrowIfNegative( delay ); |
| | | 67 | | |
| | 0 | 68 | | _settings.ReloadDelay = delay; |
| | | 69 | | |
| | 0 | 70 | | return this; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <inheritdoc /> |
| | | 74 | | public IEFConfigurationBuilder<TSettingsTable, TDbContext> WithReloader() |
| | | 75 | | { |
| | 24 | 76 | | _writerType = typeof( EFConfigurationWriter<TSettingsTable, TDbContext> ); |
| | | 77 | | |
| | 24 | 78 | | return this; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <inheritdoc /> |
| | | 82 | | public IEFConfigurationBuilder<TSettingsTable, TDbContext> WithReloader< TWriter >() |
| | | 83 | | where TWriter : IEFConfigurationWriter<TSettingsTable, TDbContext> |
| | | 84 | | { |
| | 4 | 85 | | _writerType = typeof( TWriter ); |
| | | 86 | | |
| | 4 | 87 | | return this; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <inheritdoc /> |
| | | 91 | | public IEFConfigurationBuilder<TSettingsTable, TDbContext> WithReloadOnChange( bool reload ) |
| | | 92 | | { |
| | 4 | 93 | | _settings.ReloadOnChange = reload; |
| | | 94 | | |
| | 4 | 95 | | return this; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | |
| | | 99 | | private void RegisterReloader( IServiceCollection services, Type writerType ) |
| | | 100 | | { |
| | 28 | 101 | | var settings = _settings; |
| | | 102 | | |
| | | 103 | | |
| | 28 | 104 | | services.AddScoped<IEFConfigurationWriter<TSettingsTable, TDbContext>>( sp => writerType == typeof( EFConfigurat |
| | 28 | 105 | | ? new EFConfigurationWriter<TSe |
| | 28 | 106 | | : (IEFConfigurationWriter<TSett |
| | | 107 | | |
| | 28 | 108 | | services.AddScoped<IEFConfigurationService<TSettingsTable, TDbContext>>( sp => new EFConfigurationService<TSetti |
| | 28 | 109 | | sp.GetRequiredService<IConfiguratio |
| | 28 | 110 | | sp.GetRequiredService<IEFConfigurat |
| | 28 | 111 | | } |
| | | 112 | | } |