Skip to content

Commit dc2513d

Browse files
committed
code cleanup
1 parent e4925d8 commit dc2513d

File tree

3 files changed

+9
-17
lines changed

3 files changed

+9
-17
lines changed

src/SampleDotnet.RepositoryFactory/UnitOfWork.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using System.Collections.Concurrent;
2-
using System.Collections.Generic;
32

43
namespace SampleDotnet.RepositoryFactory;
54

65
internal class UnitOfWork : IUnitOfWork
76
{
8-
private readonly Queue<DbContextId> _dbContextPool = new();
7+
private readonly Queue<DbContextId> _dbContextQueue = new();
98
private readonly ConcurrentDictionary<DbContextId, IRepository> _repositoryPool = new();
109
private readonly SemaphoreSlim _semaphoreSlim = new(1, 1);
1110
private readonly IServiceProvider _serviceProvider;
@@ -27,7 +26,7 @@ public IRepository<TDbContext> CreateRepository<TDbContext>(System.Transactions.
2726
.GetRequiredService<IDbContextFactory<TDbContext>>();
2827

2928
var repository = new Repository<TDbContext>(dbContext, transactionScopeOption, isolationLevel);
30-
_dbContextPool.Enqueue(repository.DbContext.ContextId);
29+
_dbContextQueue.Enqueue(repository.DbContext.ContextId);
3130
_repositoryPool.TryAdd(repository.DbContext.ContextId, repository);
3231
return repository;
3332
}
@@ -50,8 +49,8 @@ public async Task<bool> SaveChangesAsync(CancellationToken cancellationToken = d
5049
{
5150
await _semaphoreSlim.WaitAsync(cancellationToken);
5251

53-
int count = _dbContextPool.Count;
54-
foreach (var dbContextKey in _dbContextPool)
52+
int count = _dbContextQueue.Count;
53+
foreach (var dbContextKey in _dbContextQueue)
5554
{
5655
if (_repositoryPool.TryGetValue(dbContextKey, out var repo))
5756
{
@@ -65,7 +64,7 @@ public async Task<bool> SaveChangesAsync(CancellationToken cancellationToken = d
6564
{
6665
thrownExceptionDbContext = repo.DbContext;
6766

68-
foreach (var dbContextKey2 in _dbContextPool)
67+
foreach (var dbContextKey2 in _dbContextQueue)
6968
{
7069
if (_repositoryPool.TryGetValue(dbContextKey2, out var repo2))
7170
{
@@ -79,12 +78,12 @@ public async Task<bool> SaveChangesAsync(CancellationToken cancellationToken = d
7978

8079
for (int i = 0; i < count; i++)
8180
{
82-
if (_dbContextPool.TryDequeue(out var dbContextKey) && dbContextKey != null)
81+
if (_dbContextQueue.TryDequeue(out var dbContextKey) && dbContextKey != null)
8382
{
8483
if (_repositoryPool.TryRemove(dbContextKey, out var repo))
8584
{
8685
var newDbContext = repo.RefreshDbContext();
87-
_dbContextPool.Enqueue(newDbContext.ContextId);
86+
_dbContextQueue.Enqueue(newDbContext.ContextId);
8887
_repositoryPool.TryAdd(newDbContext.ContextId, repo);
8988
}
9089
}
@@ -116,7 +115,7 @@ protected virtual void Dispose(bool disposing)
116115
}
117116
}
118117

119-
_dbContextPool.Clear();
118+
_dbContextQueue.Clear();
120119
_semaphoreSlim.Dispose();
121120
}
122121
disposedValue = true;

test/SampleDotnet.RepositoryFactory.Tests/Cases/DbContextDisposeTests.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace SampleDotnet.RepositoryFactory.Tests.Cases
1+
namespace SampleDotnet.RepositoryFactory.Tests.Cases
82
{
93
public class DbContextDisposeTests
104
{

test/SampleDotnet.RepositoryFactory.Tests/_GlobalUsing.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66
global using Shouldly;
77
global using System.ComponentModel.DataAnnotations;
88
global using System.ComponentModel.DataAnnotations.Schema;
9-
global using System.Transactions;
109
global using Xunit;

0 commit comments

Comments
 (0)