Skip to content

Commit

Permalink
Dispose ActiveDbContext (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
truongtphat authored Sep 16, 2024
1 parent 9b80aa6 commit 712f1fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 37 deletions.
6 changes: 1 addition & 5 deletions src/mix.heart/UnitOfWork/GenericUnitOfWorkInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

namespace Mix.Heart.UnitOfWork
{
public class UnitOfWorkInfo<T> : UnitOfWorkInfo
where T : DbContext
public class UnitOfWorkInfo<T>(T dbContext) : UnitOfWorkInfo(dbContext) where T : DbContext
{
public T DbContext => (T)ActiveDbContext;
public UnitOfWorkInfo(T dbContext) : base(dbContext)
{
}
}
}
58 changes: 26 additions & 32 deletions src/mix.heart/UnitOfWork/UnitOfWorkInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,83 +32,82 @@ public void Begin()
}
}

/// <summary>
/// TODO: implement multiple db context
/// </summary>
public void Close()
{
//ActiveDbContext.Dispose();
ActiveTransaction?.Dispose();
}

/// <summary>
/// TODO: implement multiple db context
/// </summary>
public async Task CloseAsync()
{
//if (ActiveDbContext != null)
// await ActiveDbContext.DisposeAsync();
if (ActiveTransaction != null)
{
await ActiveTransaction.DisposeAsync();
}
}

/// <summary>
/// TODO: implement multiple db context
/// </summary>
public void Complete()
{
if (ActiveDbContext != null && ActiveTransaction != null)
{
ActiveDbContext.SaveChanges();
ActiveTransaction?.Commit();
ActiveTransaction?.Dispose();
ActiveTransaction.Commit();

ActiveTransaction.Dispose();
ActiveDbContext.Dispose();

ActiveTransaction = null;
ActiveDbContext = null;
}
}
/// <summary>
/// TODO: implement multiple db context
/// </summary>

public void Rollback()
{
if (ActiveDbContext != null && ActiveTransaction != null)
{
ActiveTransaction?.Rollback();
ActiveTransaction?.Dispose();
ActiveTransaction.Rollback();

ActiveTransaction.Dispose();
ActiveDbContext.Dispose();

ActiveTransaction = null;
ActiveDbContext = null;
}
}

/// <summary>
/// TODO: implement multiple db context
/// </summary>
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;
}
}
/// <summary>
/// TODO: implement multiple db context
/// </summary>

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();
}
Expand Down

0 comments on commit 712f1fb

Please sign in to comment.