1
1
using System . Collections . Concurrent ;
2
- using System . Collections . Generic ;
3
2
4
3
namespace SampleDotnet . RepositoryFactory ;
5
4
6
5
internal class UnitOfWork : IUnitOfWork
7
6
{
8
- private readonly Queue < DbContextId > _dbContextPool = new ( ) ;
7
+ private readonly Queue < DbContextId > _dbContextQueue = new ( ) ;
9
8
private readonly ConcurrentDictionary < DbContextId , IRepository > _repositoryPool = new ( ) ;
10
9
private readonly SemaphoreSlim _semaphoreSlim = new ( 1 , 1 ) ;
11
10
private readonly IServiceProvider _serviceProvider ;
@@ -27,7 +26,7 @@ public IRepository<TDbContext> CreateRepository<TDbContext>(System.Transactions.
27
26
. GetRequiredService < IDbContextFactory < TDbContext > > ( ) ;
28
27
29
28
var repository = new Repository < TDbContext > ( dbContext , transactionScopeOption , isolationLevel ) ;
30
- _dbContextPool . Enqueue ( repository . DbContext . ContextId ) ;
29
+ _dbContextQueue . Enqueue ( repository . DbContext . ContextId ) ;
31
30
_repositoryPool . TryAdd ( repository . DbContext . ContextId , repository ) ;
32
31
return repository ;
33
32
}
@@ -50,8 +49,8 @@ public async Task<bool> SaveChangesAsync(CancellationToken cancellationToken = d
50
49
{
51
50
await _semaphoreSlim . WaitAsync ( cancellationToken ) ;
52
51
53
- int count = _dbContextPool . Count ;
54
- foreach ( var dbContextKey in _dbContextPool )
52
+ int count = _dbContextQueue . Count ;
53
+ foreach ( var dbContextKey in _dbContextQueue )
55
54
{
56
55
if ( _repositoryPool . TryGetValue ( dbContextKey , out var repo ) )
57
56
{
@@ -65,7 +64,7 @@ public async Task<bool> SaveChangesAsync(CancellationToken cancellationToken = d
65
64
{
66
65
thrownExceptionDbContext = repo . DbContext ;
67
66
68
- foreach ( var dbContextKey2 in _dbContextPool )
67
+ foreach ( var dbContextKey2 in _dbContextQueue )
69
68
{
70
69
if ( _repositoryPool . TryGetValue ( dbContextKey2 , out var repo2 ) )
71
70
{
@@ -79,12 +78,12 @@ public async Task<bool> SaveChangesAsync(CancellationToken cancellationToken = d
79
78
80
79
for ( int i = 0 ; i < count ; i ++ )
81
80
{
82
- if ( _dbContextPool . TryDequeue ( out var dbContextKey ) && dbContextKey != null )
81
+ if ( _dbContextQueue . TryDequeue ( out var dbContextKey ) && dbContextKey != null )
83
82
{
84
83
if ( _repositoryPool . TryRemove ( dbContextKey , out var repo ) )
85
84
{
86
85
var newDbContext = repo . RefreshDbContext ( ) ;
87
- _dbContextPool . Enqueue ( newDbContext . ContextId ) ;
86
+ _dbContextQueue . Enqueue ( newDbContext . ContextId ) ;
88
87
_repositoryPool . TryAdd ( newDbContext . ContextId , repo ) ;
89
88
}
90
89
}
@@ -116,7 +115,7 @@ protected virtual void Dispose(bool disposing)
116
115
}
117
116
}
118
117
119
- _dbContextPool . Clear ( ) ;
118
+ _dbContextQueue . Clear ( ) ;
120
119
_semaphoreSlim . Dispose ( ) ;
121
120
}
122
121
disposedValue = true ;
0 commit comments