Skip to content

Commit 32501be

Browse files
committed
Fix: Removal of Attach and Detach and rename to IToUnitOfWorkRepository
1 parent 66cbe69 commit 32501be

File tree

4 files changed

+8
-18
lines changed

4 files changed

+8
-18
lines changed

src/CodeOfChaos.Types.UnitOfWork.Contracts/ICanAttachToUnitOfWork.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,4 @@ namespace CodeOfChaos.Types.UnitOfWork;
55
// ---------------------------------------------------------------------------------------------------------------------
66
// Code
77
// ---------------------------------------------------------------------------------------------------------------------
8-
public interface ICanAttachToUnitOfWork {
9-
void Attach(IUnitOfWork unitOfWork);
10-
void Detach(IUnitOfWork unitOfWork);
11-
}
8+
public interface IToUnitOfWorkRepository;

src/CodeOfChaos.Types.UnitOfWork.Contracts/IUnitOfWork.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ public interface IUnitOfWork : IAsyncDisposable {
2020

2121
ValueTask<TDbContext> GetDbContextAsync<TDbContext>(CancellationToken ct = default) where TDbContext : DbContext;
2222

23-
TRepo GetRepository<TRepo>() where TRepo : class, ICanAttachToUnitOfWork;
23+
TRepo GetRepository<TRepo>() where TRepo : class, IToUnitOfWorkRepository;
2424
}

src/CodeOfChaos.Types.UnitOfWork/UnitOfWork.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace CodeOfChaos.Types.UnitOfWork;
1414
public class UnitOfWork<TDbContext>(IDbContextFactory<TDbContext> dbContextFactory, IServiceScope serviceScope) : IUnitOfWork where TDbContext : DbContext{
1515
private readonly AsyncLazy<TDbContext> _db = new(async ct => await dbContextFactory.CreateDbContextAsync(ct));
1616
private IDbContextTransaction? _transaction;
17-
private ConcurrentDictionary<Type, ICanAttachToUnitOfWork> AttachedRepositories { get; } = [];
17+
private ConcurrentDictionary<Type, IToUnitOfWorkRepository> AttachedRepositories { get; } = [];
1818

1919
// -----------------------------------------------------------------------------------------------------------------
2020
// Methods
@@ -84,13 +84,12 @@ public virtual async ValueTask<T> GetDbContextAsync<T>(CancellationToken ct = de
8484
return dbContext as T ?? throw new InvalidCastException($"Cannot cast DbContext of type '{dbContext.GetType()}' to '{typeof(T)}'");
8585
}
8686

87-
public virtual TRepo GetRepository<TRepo>() where TRepo : class, ICanAttachToUnitOfWork {
88-
if (AttachedRepositories.TryGetValue(typeof(TRepo), out ICanAttachToUnitOfWork? cachedRepo) && cachedRepo is TRepo castedCachedRepo) return castedCachedRepo;
87+
public virtual TRepo GetRepository<TRepo>() where TRepo : class, IToUnitOfWorkRepository {
88+
if (AttachedRepositories.TryGetValue(typeof(TRepo), out IToUnitOfWorkRepository? cachedRepo) && cachedRepo is TRepo castedCachedRepo) return castedCachedRepo;
8989

9090
// Cache miss so we create a new instance
9191
var repo = serviceScope.ServiceProvider.GetRequiredService<TRepo>();
9292

93-
repo.Attach(this);
9493
AttachedRepositories.AddOrUpdate(typeof(TRepo), repo);
9594
return repo;
9695
}
@@ -99,12 +98,6 @@ public virtual async ValueTask DisposeAsync() {
9998
if (_transaction != null) await TryRollbackTransactionAsync();
10099

101100
if (!AttachedRepositories.IsEmpty) {
102-
// First detach all references to this unit of work
103-
foreach ((_, ICanAttachToUnitOfWork repo) in AttachedRepositories) {
104-
repo.Detach(this);
105-
}
106-
107-
// Then clear our own reference to them
108101
AttachedRepositories.Clear();
109102
}
110103

tests/Tests.CodeOfChaos.Types.UnitOfWork/UnitOfWorkTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,14 @@ public async Task GetDbContextAsync_ShouldThrowForUnsupportedDbContextType() {
193193
[Test]
194194
public async Task GetRepository_ShouldRetrieveRepositoryFromServiceProvider() {
195195
// Arrange
196-
var mockRepository = new Mock<ICanAttachToUnitOfWork>();
196+
var mockRepository = new Mock<IToUnitOfWorkRepository>();
197197
_serviceProvider
198-
.Setup(sp => sp.GetService(typeof(ICanAttachToUnitOfWork)))
198+
.Setup(sp => sp.GetService(typeof(IToUnitOfWorkRepository)))
199199
.Returns(mockRepository.Object);
200200

201201

202202
// Act
203-
var repository = _unitOfWork.GetRepository<ICanAttachToUnitOfWork>();
203+
var repository = _unitOfWork.GetRepository<IToUnitOfWorkRepository>();
204204

205205
// Assert
206206
await Assert.That(repository).IsNotNull();

0 commit comments

Comments
 (0)