| | | 1 | | namespace ZhEaIsNsAaBn.Infrastructure.Core; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Default <see cref="IDbContextProvider{TDbContext}"/>. Wraps the context DI gives it (scoped registration), or create |
| | | 5 | | /// contexts from an <see cref="IDbContextFactory{TContext}"/> (pooled registration). |
| | | 6 | | /// </summary> |
| | | 7 | | /// <remarks> |
| | | 8 | | /// The shared context is created once per provider. A shared context the provider created from the factory is disposed |
| | | 9 | | /// with the provider (for a pool this returns it to the pool); a context DI gave it is left to DI. |
| | | 10 | | /// </remarks> |
| | | 11 | | /// <typeparam name="TDbContext">The application's context.</typeparam> |
| | | 12 | | public class DbContextProvider< TDbContext > : IDbContextProvider<TDbContext>, IDisposable, IAsyncDisposable |
| | | 13 | | where TDbContext : DbContext |
| | | 14 | | { |
| | | 15 | | private readonly IDbContextFactory<TDbContext>? _dbContextFactory; |
| | 336 | 16 | | private readonly SemaphoreSlim _getDbContextLock = new( 1, 1 ); |
| | | 17 | | private TDbContext? _dbContext; |
| | | 18 | | private bool _ownsDbContext; |
| | | 19 | | private bool _disposed; |
| | | 20 | | |
| | | 21 | | |
| | | 22 | | /// <summary>Creates a provider that always hands out <paramref name="dbContext"/> as the shared context.</summary> |
| | | 23 | | /// <param name="dbContext">The context from DI.</param> |
| | | 24 | | /// <param name="dbContextFactory">Optional factory for <c>singleInstance: false</c>; without one, asking for a new |
| | | 25 | | /// <exception cref="ArgumentNullException"><paramref name="dbContext"/> is <see langword="null"/>.</exception> |
| | 296 | 26 | | public DbContextProvider( TDbContext dbContext, IDbContextFactory<TDbContext>? dbContextFactory = null ) |
| | | 27 | | { |
| | 296 | 28 | | ArgumentNullException.ThrowIfNull( dbContext ); |
| | | 29 | | |
| | 296 | 30 | | _dbContext = dbContext; |
| | 296 | 31 | | _dbContextFactory = dbContextFactory; |
| | 296 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary>Creates a provider that takes every context from <paramref name="dbContextFactory"/>.</summary> |
| | | 35 | | /// <param name="dbContextFactory">The factory, e.g. from <c>AddPooledDbContextFactory</c>.</param> |
| | | 36 | | /// <exception cref="ArgumentNullException"><paramref name="dbContextFactory"/> is <see langword="null"/>.</exceptio |
| | 40 | 37 | | public DbContextProvider( IDbContextFactory<TDbContext> dbContextFactory ) |
| | | 38 | | { |
| | 40 | 39 | | ArgumentNullException.ThrowIfNull( dbContextFactory ); |
| | | 40 | | |
| | 40 | 41 | | _dbContextFactory = dbContextFactory; |
| | 40 | 42 | | } |
| | | 43 | | |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public virtual TDbContext GetDbContext( bool singleInstance = true, CancellationToken cancellationToken = default ) |
| | | 47 | | { |
| | 300 | 48 | | ObjectDisposedException.ThrowIf( _disposed, this ); |
| | | 49 | | |
| | 304 | 50 | | if ( !singleInstance ) { return RequireFactory().CreateDbContext(); } |
| | | 51 | | |
| | | 52 | | |
| | 592 | 53 | | if ( _dbContext is not null ) { return _dbContext; } |
| | | 54 | | |
| | | 55 | | |
| | 0 | 56 | | var factory = RequireFactory(); |
| | | 57 | | |
| | | 58 | | |
| | 0 | 59 | | _getDbContextLock.Wait( cancellationToken ); |
| | | 60 | | |
| | | 61 | | try |
| | | 62 | | { |
| | 0 | 63 | | if ( _dbContext is null ) |
| | | 64 | | { |
| | 0 | 65 | | _dbContext = factory.CreateDbContext(); |
| | 0 | 66 | | _ownsDbContext = true; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | |
| | 0 | 70 | | return _dbContext; |
| | | 71 | | } |
| | 0 | 72 | | finally { _getDbContextLock.Release(); } |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <inheritdoc /> |
| | | 76 | | public virtual async Task<TDbContext> GetDbContextAsync( bool singleInstance = true, CancellationToken cancellationT |
| | | 77 | | { |
| | 316 | 78 | | ObjectDisposedException.ThrowIf( _disposed, this ); |
| | | 79 | | |
| | 332 | 80 | | if ( !singleInstance ) { return await RequireFactory().CreateDbContextAsync( cancellationToken ).ConfigureAwait( |
| | | 81 | | |
| | | 82 | | |
| | 568 | 83 | | if ( _dbContext is not null ) { return _dbContext; } |
| | | 84 | | |
| | | 85 | | |
| | 32 | 86 | | var factory = RequireFactory(); |
| | | 87 | | |
| | | 88 | | |
| | 32 | 89 | | await _getDbContextLock.WaitAsync( cancellationToken ).ConfigureAwait( false ); |
| | | 90 | | |
| | | 91 | | try |
| | | 92 | | { |
| | 28 | 93 | | if ( _dbContext is null ) |
| | | 94 | | { |
| | 24 | 95 | | _dbContext = await factory.CreateDbContextAsync( cancellationToken ).ConfigureAwait( false ); |
| | 24 | 96 | | _ownsDbContext = true; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | |
| | 28 | 100 | | return _dbContext; |
| | | 101 | | } |
| | 28 | 102 | | finally { _getDbContextLock.Release(); } |
| | 312 | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary>Disposes the shared context if this provider created it.</summary> |
| | | 106 | | public void Dispose() |
| | | 107 | | { |
| | 24 | 108 | | Dispose( true ); |
| | 24 | 109 | | GC.SuppressFinalize( this ); |
| | 24 | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary>Async version of <see cref="Dispose()"/>.</summary> |
| | | 113 | | /// <returns>A task that completes when the shared context is disposed.</returns> |
| | | 114 | | public async ValueTask DisposeAsync() |
| | | 115 | | { |
| | 76 | 116 | | if ( !_disposed && _ownsDbContext && _dbContext is not null ) { await _dbContext.DisposeAsync().ConfigureAwait( |
| | | 117 | | |
| | | 118 | | |
| | 56 | 119 | | _ownsDbContext = false; |
| | 56 | 120 | | Dispose( true ); |
| | 56 | 121 | | GC.SuppressFinalize( this ); |
| | 56 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary>Releases the provider's resources.</summary> |
| | | 125 | | /// <param name="disposing"><see langword="true"/> when called from <see cref="Dispose()"/>.</param> |
| | | 126 | | protected virtual void Dispose( bool disposing ) |
| | | 127 | | { |
| | 80 | 128 | | if ( _disposed ) { return; } |
| | | 129 | | |
| | | 130 | | |
| | 80 | 131 | | if ( disposing ) |
| | | 132 | | { |
| | 80 | 133 | | if ( _ownsDbContext ) { _dbContext?.Dispose(); } |
| | | 134 | | |
| | | 135 | | |
| | 80 | 136 | | _getDbContextLock.Dispose(); |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | |
| | 80 | 140 | | _dbContext = null; |
| | 80 | 141 | | _disposed = true; |
| | 80 | 142 | | } |
| | | 143 | | |
| | | 144 | | |
| | | 145 | | private IDbContextFactory<TDbContext> RequireFactory() |
| | 52 | 146 | | => _dbContextFactory ?? |
| | 52 | 147 | | throw new InvalidOperationException( $"No IDbContextFactory<{typeof( TDbContext ).Name}> is registered. Regis |
| | | 148 | | } |