Skip to content

Commit 66cbe69

Browse files
committed
Fix: ICanAttachToUnitOfWork instead of IRepository
1 parent 8d3a064 commit 66cbe69

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

src/CodeOfChaos.Types.UnitOfWork.Contracts/IRepository.cs renamed to src/CodeOfChaos.Types.UnitOfWork.Contracts/ICanAttachToUnitOfWork.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace CodeOfChaos.Types.UnitOfWork;
55
// ---------------------------------------------------------------------------------------------------------------------
66
// Code
77
// ---------------------------------------------------------------------------------------------------------------------
8-
public interface IRepository {
8+
public interface ICanAttachToUnitOfWork {
99
void Attach(IUnitOfWork unitOfWork);
1010
void Detach(IUnitOfWork unitOfWork);
1111
}

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, IRepository;
23+
TRepo GetRepository<TRepo>() where TRepo : class, ICanAttachToUnitOfWork;
2424
}

src/CodeOfChaos.Types.UnitOfWork/UnitOfWork.cs

Lines changed: 4 additions & 4 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, IRepository> AttachedRepositories { get; } = [];
17+
private ConcurrentDictionary<Type, ICanAttachToUnitOfWork> AttachedRepositories { get; } = [];
1818

1919
// -----------------------------------------------------------------------------------------------------------------
2020
// Methods
@@ -84,8 +84,8 @@ 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, IRepository {
88-
if (AttachedRepositories.TryGetValue(typeof(TRepo), out IRepository? cachedRepo) && cachedRepo is TRepo castedCachedRepo) return castedCachedRepo;
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;
8989

9090
// Cache miss so we create a new instance
9191
var repo = serviceScope.ServiceProvider.GetRequiredService<TRepo>();
@@ -100,7 +100,7 @@ public virtual async ValueTask DisposeAsync() {
100100

101101
if (!AttachedRepositories.IsEmpty) {
102102
// First detach all references to this unit of work
103-
foreach ((_, IRepository repo) in AttachedRepositories) {
103+
foreach ((_, ICanAttachToUnitOfWork repo) in AttachedRepositories) {
104104
repo.Detach(this);
105105
}
106106

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<IRepository>();
196+
var mockRepository = new Mock<ICanAttachToUnitOfWork>();
197197
_serviceProvider
198-
.Setup(sp => sp.GetService(typeof(IRepository)))
198+
.Setup(sp => sp.GetService(typeof(ICanAttachToUnitOfWork)))
199199
.Returns(mockRepository.Object);
200200

201201

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

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

0 commit comments

Comments
 (0)