@@ -14,7 +14,7 @@ namespace CodeOfChaos.Types.UnitOfWork;
1414public 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
0 commit comments