From 712f1fb4be5833e3865d0ef6308c8a0b71ab7fd9 Mon Sep 17 00:00:00 2001 From: Phat Truong Date: Mon, 16 Sep 2024 18:09:50 +0700 Subject: [PATCH] Dispose ActiveDbContext (#43) --- .../UnitOfWork/GenericUnitOfWorkInfo.cs | 6 +- src/mix.heart/UnitOfWork/UnitOfWorkInfo.cs | 58 +++++++++---------- 2 files changed, 27 insertions(+), 37 deletions(-) diff --git a/src/mix.heart/UnitOfWork/GenericUnitOfWorkInfo.cs b/src/mix.heart/UnitOfWork/GenericUnitOfWorkInfo.cs index 0bf9776..4bd34f9 100644 --- a/src/mix.heart/UnitOfWork/GenericUnitOfWorkInfo.cs +++ b/src/mix.heart/UnitOfWork/GenericUnitOfWorkInfo.cs @@ -2,12 +2,8 @@ namespace Mix.Heart.UnitOfWork { - public class UnitOfWorkInfo : UnitOfWorkInfo - where T : DbContext + public class UnitOfWorkInfo(T dbContext) : UnitOfWorkInfo(dbContext) where T : DbContext { public T DbContext => (T)ActiveDbContext; - public UnitOfWorkInfo(T dbContext) : base(dbContext) - { - } } } diff --git a/src/mix.heart/UnitOfWork/UnitOfWorkInfo.cs b/src/mix.heart/UnitOfWork/UnitOfWorkInfo.cs index e113230..8b60061 100644 --- a/src/mix.heart/UnitOfWork/UnitOfWorkInfo.cs +++ b/src/mix.heart/UnitOfWork/UnitOfWorkInfo.cs @@ -6,17 +6,12 @@ namespace Mix.Heart.UnitOfWork { - public class UnitOfWorkInfo : IUnitOfWorkInfo + public class UnitOfWorkInfo(DbContext dbContext) : IUnitOfWorkInfo { - public DbContext ActiveDbContext { get; private set; } + public DbContext ActiveDbContext { get; private set; } = dbContext; public IDbContextTransaction ActiveTransaction { get; private set; } - public UnitOfWorkInfo(DbContext dbContext) - { - ActiveDbContext = dbContext; - } - public void SetDbContext(DbContext dbContext) { ActiveDbContext = dbContext; @@ -37,83 +32,82 @@ public void Begin() } } - /// - /// TODO: implement multiple db context - /// public void Close() { - //ActiveDbContext.Dispose(); ActiveTransaction?.Dispose(); } - /// - /// TODO: implement multiple db context - /// public async Task CloseAsync() { - //if (ActiveDbContext != null) - // await ActiveDbContext.DisposeAsync(); if (ActiveTransaction != null) { await ActiveTransaction.DisposeAsync(); } } - /// - /// TODO: implement multiple db context - /// public void Complete() { if (ActiveDbContext != null && ActiveTransaction != null) { ActiveDbContext.SaveChanges(); - ActiveTransaction?.Commit(); - ActiveTransaction?.Dispose(); + ActiveTransaction.Commit(); + + ActiveTransaction.Dispose(); + ActiveDbContext.Dispose(); + ActiveTransaction = null; + ActiveDbContext = null; } } - /// - /// TODO: implement multiple db context - /// + public void Rollback() { if (ActiveDbContext != null && ActiveTransaction != null) { - ActiveTransaction?.Rollback(); - ActiveTransaction?.Dispose(); + ActiveTransaction.Rollback(); + + ActiveTransaction.Dispose(); + ActiveDbContext.Dispose(); + ActiveTransaction = null; + ActiveDbContext = null; } } - /// - /// TODO: implement multiple db context - /// public async Task CompleteAsync(CancellationToken cancellationToken = default) { if (ActiveDbContext != null && ActiveTransaction != null) { await ActiveDbContext.SaveChangesAsync(cancellationToken); await ActiveTransaction.CommitAsync(cancellationToken); + await ActiveTransaction.DisposeAsync(); + await ActiveDbContext.DisposeAsync(); + ActiveTransaction = null; + ActiveDbContext = null; } } - /// - /// TODO: implement multiple db context - /// + public async Task RollbackAsync(CancellationToken cancellationToken = default) { if (ActiveDbContext != null && ActiveTransaction != null) { await ActiveTransaction.RollbackAsync(cancellationToken); + await ActiveTransaction.DisposeAsync(); + await ActiveDbContext.DisposeAsync(); + ActiveTransaction = null; + ActiveDbContext = null; } } public void Dispose() { + ActiveTransaction?.Dispose(); ActiveDbContext?.Dispose(); + GC.SuppressFinalize(this); GC.WaitForPendingFinalizers(); }