| | | 1 | | namespace ZhEaIsNsAaBn.Infrastructure.Core; |
| | | 2 | | |
| | | 3 | | /// <summary>Root builder: keeps the recorded registrations and runs them in <see cref="Build"/>.</summary> |
| | | 4 | | internal sealed class InfrastructureBuilder : IInfrastructureBuilder |
| | | 5 | | { |
| | 132 | 6 | | private readonly List<Action<IServiceCollection>> _registrations = []; |
| | | 7 | | |
| | | 8 | | |
| | 132 | 9 | | internal InfrastructureBuilder( IServiceCollection services ) |
| | | 10 | | { |
| | 132 | 11 | | ArgumentNullException.ThrowIfNull( services ); |
| | | 12 | | |
| | | 13 | | Services = services; |
| | 132 | 14 | | } |
| | | 15 | | |
| | | 16 | | |
| | | 17 | | internal IServiceCollection Services { get; } |
| | | 18 | | |
| | | 19 | | |
| | | 20 | | public IServiceCollection Build() |
| | | 21 | | { |
| | 712 | 22 | | foreach ( var registration in _registrations ) { registration( Services ); } |
| | | 23 | | |
| | | 24 | | |
| | 116 | 25 | | _registrations.Clear(); |
| | | 26 | | |
| | 116 | 27 | | return Services; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | public IInfrastructureBuilder<TDbContext> WithDbContext< TDbContext >( Action<DbContextOptionsBuilder> dbContextOpti |
| | | 31 | | where TDbContext : DbContext |
| | | 32 | | { |
| | 152 | 33 | | ArgumentNullException.ThrowIfNull( dbContextOptionsBuilder ); |
| | | 34 | | |
| | 152 | 35 | | var builder = new InfrastructureBuilder<TDbContext>( this, dbContextOptionsBuilder ); |
| | | 36 | | |
| | | 37 | | |
| | 152 | 38 | | Record( builder.Register ); |
| | | 39 | | |
| | 152 | 40 | | return builder; |
| | | 41 | | } |
| | | 42 | | |
| | 180 | 43 | | internal void Record( Action<IServiceCollection> registration ) => _registrations.Add( registration ); |
| | | 44 | | } |