Skip to content

Commit 1ef5e74

Browse files
committed
Fix: IUnitOfWorkRepository
1 parent 32501be commit 1ef5e74

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ namespace CodeOfChaos.Types.UnitOfWork;
55
// ---------------------------------------------------------------------------------------------------------------------
66
// Code
77
// ---------------------------------------------------------------------------------------------------------------------
8-
public interface IToUnitOfWorkRepository;
8+
public interface IUnitOfWorkRepository;

src/CodeOfChaos.Types.UnitOfWork/UnitOfWork.cs

Lines changed: 3 additions & 3 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, IToUnitOfWorkRepository> AttachedRepositories { get; } = [];
17+
private ConcurrentDictionary<Type, IUnitOfWorkRepository> 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, IToUnitOfWorkRepository {
88-
if (AttachedRepositories.TryGetValue(typeof(TRepo), out IToUnitOfWorkRepository? cachedRepo) && cachedRepo is TRepo castedCachedRepo) return castedCachedRepo;
87+
public virtual TRepo GetRepository<TRepo>() where TRepo : class, IUnitOfWorkRepository {
88+
if (AttachedRepositories.TryGetValue(typeof(TRepo), out IUnitOfWorkRepository? cachedRepo) && cachedRepo is TRepo castedCachedRepo) return castedCachedRepo;
8989

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

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

201201

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

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

0 commit comments

Comments
 (0)