< Summary - ZhEaIsNsAaBn.Libraries coverage (affected: 23 packages)

Information
Class: ZhEaIsNsAaBn.APPSettings.Builders.EFConfigurationBuilder<T1, T2>
Assembly: ZhEaIsNsAaBn.APPSettings
File(s): /home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/DataBasePackages/ZhEaIsNsAaBn.APPSettings/Builders/EFConfigurationBuilder.cs
Line coverage
94%
Covered lines: 33
Uncovered lines: 2
Coverable lines: 35
Total lines: 112
Line coverage: 94.2%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Build(...)100%1010100%
WithDbContextFactory(...)100%11100%
WithQuery(...)100%11100%
WithReloadDelay(...)100%1133.33%
WithReloader()100%11100%
WithReloadOnChange(...)100%11100%
RegisterReloader(...)100%11100%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/DataBasePackages/ZhEaIsNsAaBn.APPSettings/Builders/EFConfigurationBuilder.cs

#LineLine coverage
 1namespace ZhEaIsNsAaBn.APPSettings.Builders;
 2
 3/// <summary>Records options; <see cref="Build"/> adds the source and registers the services.</summary>
 4internal 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;
 5610    private readonly EFConfigurationProviderSettings<TSettingsTable, TDbContext> _settings = new();
 11    private          Type?                                                       _writerType;
 12
 13
 5614    public EFConfigurationBuilder( IConfigurationBuilder configuration, IServiceProvider? serviceProvider )
 15    {
 5616        _configuration   = configuration;
 5617        _serviceProvider = serviceProvider;
 5618    }
 19
 20
 21    /// <inheritdoc />
 22    public IConfigurationBuilder Build( IServiceCollection services )
 23    {
 5224        ArgumentNullException.ThrowIfNull( services );
 25
 26
 5227        _settings.DbContextFactory ??= _serviceProvider?.GetService<IDbContextFactory<TDbContext>>();
 5228        _settings.GetRequiredDbContextFactory();
 29
 30
 31        // TODO@ZhEaIsNsAaBn: automatic reload on SaveChanges — old version never fired; rebuild on the change-tracker
 5232        if ( _writerType is null && _settings.ReloadOnChange ) { WithReloader(); }
 33
 34
 7635        if ( _writerType is not null ) { RegisterReloader( services, _writerType ); }
 36
 37
 4838        _configuration.Add( new EFConfigurationSource<TSettingsTable, TDbContext>( _settings ) );
 39
 4840        return _configuration;
 41    }
 42
 43    /// <inheritdoc />
 44    public IEFConfigurationBuilder<TSettingsTable, TDbContext> WithDbContextFactory( IDbContextFactory<TDbContext> facto
 45    {
 4446        ArgumentNullException.ThrowIfNull( factory );
 47
 4448        _settings.DbContextFactory = factory;
 49
 4450        return this;
 51    }
 52
 53    /// <inheritdoc />
 54    public IEFConfigurationBuilder<TSettingsTable, TDbContext> WithQuery( Expression<Func<TSettingsTable, bool>> query )
 55    {
 456        ArgumentNullException.ThrowIfNull( query );
 57
 458        _settings.Query = query;
 59
 460        return this;
 61    }
 62
 63    /// <inheritdoc />
 64    public IEFConfigurationBuilder<TSettingsTable, TDbContext> WithReloadDelay( int delay )
 65    {
 466        ArgumentOutOfRangeException.ThrowIfNegative( delay );
 67
 068        _settings.ReloadDelay = delay;
 69
 070        return this;
 71    }
 72
 73    /// <inheritdoc />
 74    public IEFConfigurationBuilder<TSettingsTable, TDbContext> WithReloader()
 75    {
 2476        _writerType = typeof( EFConfigurationWriter<TSettingsTable, TDbContext> );
 77
 2478        return this;
 79    }
 80
 81    /// <inheritdoc />
 82    public IEFConfigurationBuilder<TSettingsTable, TDbContext> WithReloader< TWriter >()
 83        where TWriter : IEFConfigurationWriter<TSettingsTable, TDbContext>
 84    {
 485        _writerType = typeof( TWriter );
 86
 487        return this;
 88    }
 89
 90    /// <inheritdoc />
 91    public IEFConfigurationBuilder<TSettingsTable, TDbContext> WithReloadOnChange( bool reload )
 92    {
 493        _settings.ReloadOnChange = reload;
 94
 495        return this;
 96    }
 97
 98
 99    private void RegisterReloader( IServiceCollection services, Type writerType )
 100    {
 28101        var settings = _settings;
 102
 103
 28104        services.AddScoped<IEFConfigurationWriter<TSettingsTable, TDbContext>>( sp => writerType == typeof( EFConfigurat
 28105                                                                                         ? new EFConfigurationWriter<TSe
 28106                                                                                         : (IEFConfigurationWriter<TSett
 107
 28108        services.AddScoped<IEFConfigurationService<TSettingsTable, TDbContext>>( sp => new EFConfigurationService<TSetti
 28109                                                                                     sp.GetRequiredService<IConfiguratio
 28110                                                                                     sp.GetRequiredService<IEFConfigurat
 28111    }
 112}