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

Information
Class: ZhEaIsNsAaBn.Infrastructure.Core.InfrastructureBuilder<T>
Assembly: ZhEaIsNsAaBn.Infrastructure.Core
File(s): /home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/DataBasePackages/ZhEaIsNsAaBn.Infrastructure.Core/Registrations/InfrastructureBuilderOfTDbContext.cs
Line coverage
97%
Covered lines: 45
Uncovered lines: 1
Coverable lines: 46
Total lines: 140
Line coverage: 97.8%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Services()100%210%
AsCustom(...)100%11100%
AsFactoryPool(...)100%11100%
AsScoped()100%11100%
ConfigureDbContextOptions(...)100%11100%
ConfigureDbContextOptions(...)100%11100%
WithAuditing(...)100%11100%
WithSoftDeleteFilter()100%11100%
Build()100%11100%
WithRepository(...)100%11100%
WithDbContext(...)100%11100%
Register(...)75%44100%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/DataBasePackages/ZhEaIsNsAaBn.Infrastructure.Core/Registrations/InfrastructureBuilderOfTDbContext.cs

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection.Extensions;
 2
 3namespace ZhEaIsNsAaBn.Infrastructure.Core;
 4
 5/// <summary>Records the registration mode of one context; <see cref="Register"/> runs when the root builder builds.</su
 6/// <typeparam name="TDbContext">The context type.</typeparam>
 7internal sealed class InfrastructureBuilder< TDbContext > : IInfrastructureBuilder<TDbContext>
 8    where TDbContext : DbContext
 9{
 10    private readonly Action<DbContextOptionsBuilder>                         _dbContextOptionsBuilder;
 15211    private readonly List<Action<IServiceProvider, DbContextOptionsBuilder>> _extraOptions = [];
 12    private readonly InfrastructureBuilder                                   _root;
 13    private          Action<IServiceProvider, DbContextOptionsBuilder>?      _auditing;
 15214    private          ServiceLifetime                                         _contextLifetime = ServiceLifetime.Scoped;
 15    private          int?                                                    _poolSize;
 16    private          bool                                                    _softDeleteFilter;
 17
 18
 15219    internal InfrastructureBuilder( InfrastructureBuilder root, Action<DbContextOptionsBuilder> dbContextOptionsBuilder 
 20    {
 15221        _root                    = root;
 15222        _dbContextOptionsBuilder = dbContextOptionsBuilder;
 15223    }
 24
 25
 026    public IServiceCollection Services => _root.Services;
 27
 28
 29    public IInfrastructureBuilder<TDbContext> AsCustom( ServiceLifetime contextLifetime )
 30    {
 3231        _contextLifetime = contextLifetime;
 3232        _poolSize        = null;
 33
 3234        return this;
 35    }
 36
 37    public IInfrastructureBuilder<TDbContext> AsFactoryPool( int poolSize = 1024 )
 38    {
 5639        ArgumentOutOfRangeException.ThrowIfNegativeOrZero( poolSize );
 40
 5641        _poolSize = poolSize;
 42
 5643        return this;
 44    }
 45
 2446    public IInfrastructureBuilder<TDbContext> AsScoped() => AsCustom( ServiceLifetime.Scoped );
 47
 48    public IInfrastructureBuilder<TDbContext> ConfigureDbContextOptions( Action<DbContextOptionsBuilder> configure )
 49    {
 6050        ArgumentNullException.ThrowIfNull( configure );
 51
 5652        _extraOptions.Add( ( _, options ) => configure( options ) );
 53
 5654        return this;
 55    }
 56
 57    public IInfrastructureBuilder<TDbContext> ConfigureDbContextOptions( Action<IServiceProvider, DbContextOptionsBuilde
 58    {
 1659        ArgumentNullException.ThrowIfNull( configure );
 60
 1261        _extraOptions.Add( configure );
 62
 1263        return this;
 64    }
 65
 66    public IInfrastructureBuilder<TDbContext> WithAuditing< TId >( Action<AuditOptions<TId>> configure )
 67        where TId : struct, IEquatable<TId>
 68    {
 69        // Configured and checked now (fails fast); the interceptor is created with the options, in Build()'s registrati
 2070        var auditOptions = AuditingOptionsExtensions.AuditOptionsFrom( configure );
 71
 1672        _auditing = ( serviceProvider, options ) => options.AddInterceptors( new AuditInterceptor<TId>( auditOptions, se
 73
 1674        return this;
 75    }
 76
 77    public IInfrastructureBuilder<TDbContext> WithSoftDeleteFilter()
 78    {
 879        _softDeleteFilter = true;
 80
 881        return this;
 82    }
 83
 11284    public IServiceCollection Build() => _root.Build();
 85
 86    public IInfrastructureBuilder<TDbContext> WithRepository< TService, TImplementation >( ServiceLifetime lifetime = Se
 87        where TService : class
 88        where TImplementation : class, TService
 89    {
 2890        _root.Record( services =>
 2891        {
 2892            services.RemoveAll<TService>();
 2893            services.Add( new ServiceDescriptor( typeof( TService ), typeof( TImplementation ), lifetime ) );
 2894        } );
 95
 2896        return this;
 97    }
 98
 99    public IInfrastructureBuilder<TOtherDbContext> WithDbContext< TOtherDbContext >( Action<DbContextOptionsBuilder> dbC
 100        where TOtherDbContext : DbContext
 20101        => _root.WithDbContext<TOtherDbContext>( dbContextOptionsBuilder );
 102
 103    internal void Register( IServiceCollection services )
 104    {
 105        // The context's own action first, then the recorded extras (e.g. a provider's options extension), auditing, the
 132106        var extras           = _extraOptions.ToArray();
 132107        var auditing         = _auditing;
 132108        var softDeleteFilter = _softDeleteFilter;
 109
 110        void ConfigureOptions( IServiceProvider serviceProvider, DbContextOptionsBuilder options )
 111        {
 112            _dbContextOptionsBuilder( options );
 113
 114            foreach ( var extra in extras ) { extra( serviceProvider, options ); }
 115
 116            auditing?.Invoke( serviceProvider, options );
 117
 118            if ( softDeleteFilter ) { options.UseSoftDeleteFilter(); }
 119        }
 120
 121
 132122        if ( _poolSize is { } poolSize )
 123        {
 48124            services.AddPooledDbContextFactory<TDbContext>( ConfigureOptions, poolSize );
 48125            services.TryAddScoped<IDbContextProvider<TDbContext>>( sp => new DbContextProvider<TDbContext>( sp.GetRequir
 126
 127
 48128            return;
 129        }
 130
 131
 84132        var optionsLifetime = _contextLifetime == ServiceLifetime.Singleton ? ServiceLifetime.Singleton : ServiceLifetim
 133
 134
 84135        services.AddDbContext<TDbContext>( ConfigureOptions, _contextLifetime, optionsLifetime );
 84136        services.TryAdd( new ServiceDescriptor( typeof( IDbContextProvider<TDbContext> ),
 84137                                                sp => new DbContextProvider<TDbContext>( sp.GetRequiredService<TDbContex
 84138                                                _contextLifetime ) );
 84139    }
 140}