Skip to content

Commit

Permalink
Merge pull request #489 from TortugaResearch/IAsyncDisposable-488
Browse files Browse the repository at this point in the history
Add IAsyncDisposable support #488
  • Loading branch information
Grauenwolf committed Jul 14, 2022
2 parents 5d35755 + 5a3f138 commit d0fcf6a
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Tortuga.Chain/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ datasource.FromTable<TObject>(filter).ToAggregate().ToCollection().Execute();

In the second version, the table or view name is extracted from the class.

### Technical Debt

[#488 Add IAsyncDisposable support](https://github.com/TortugaResearch/Tortuga.Chain/issues/488)
Added support for `IAsyncDisposable` to transactional data sources.


## Version 4.2

### Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace Traits
{
[Trait]
class TransactionalDataSourceTrait<TRootDataSource, TConnection, TTransaction, TCommand, TDatabaseMetadata> : ITransactionalDataSource, IDisposable
#if NET6_0_OR_GREATER
, IAsyncDisposable
#endif
where TRootDataSource : class, IRootDataSource, IDataSource, IHasExtensionCache
where TConnection : DbConnection
where TTransaction : DbTransaction
Expand Down Expand Up @@ -197,6 +200,36 @@ public void Dispose(bool disposing)
}
}

#if NET6_0_OR_GREATER
/// <summary>
/// Closes the current transaction and connection. If not committed, the transaction is rolled back.
/// </summary>
[Expose]
public ValueTask DisposeAsync()
{
return DisposeAsync(true);
}

/// <summary>
/// Closes the current transaction and connection. If not committed, the transaction is rolled back.
/// </summary>
/// <param name="disposing"></param>
[Expose(Accessibility = Accessibility.Protected, Inheritance = Inheritance.Virtual)]
public async ValueTask DisposeAsync(bool disposing)
{
if (m_Disposed)
return;

if (disposing)
{
await m_Transaction.DisposeAsync().ConfigureAwait(false);
await m_Connection.DisposeAsync().ConfigureAwait(false);
DisposableContainer?.OnDispose();
m_Disposed = true;
}
}
#endif

/// <summary>
/// The extension cache is used by extensions to store data source specific information.
/// </summary>
Expand Down Expand Up @@ -261,4 +294,4 @@ public TDatabaseMetadata DatabaseMetadata
get { return (TDatabaseMetadata)m_BaseDataSource.DatabaseMetadata; }
}
}
}
}
55 changes: 53 additions & 2 deletions Tortuga.Chain/Shared/Tests/Core/DataSourceTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using Tortuga.Chain.DataSources;

namespace Tests.Core;

[TestClass]
Expand Down Expand Up @@ -31,4 +32,54 @@ public async Task TestConnectionAsync(string dataSourceName, DataSourceType mode
Release(dataSource);
}
}
}

[DataTestMethod, RootData(DataSourceGroup.Primary)]
public void Transaction_Dispose(string dataSourceName)
{
var dataSource = DataSource(dataSourceName);
try
{
dataSource.TestConnection();
var trans = ((IRootDataSource)dataSource).BeginTransaction();
trans.Dispose();
}
finally
{
Release(dataSource);
}
}

#if NET6_0_OR_GREATER

[DataTestMethod, RootData(DataSourceGroup.Primary)]
public async Task Transaction_DisposeAsync_Using(string dataSourceName)
{
var dataSource = DataSource(dataSourceName);
try
{
dataSource.TestConnection();
await using var trans = await ((IRootDataSource)dataSource).BeginTransactionAsync();
}
finally
{
Release(dataSource);
}
}

[DataTestMethod, RootData(DataSourceGroup.Primary)]
public async Task Transaction_DisposeAsync(string dataSourceName)
{
var dataSource = DataSource(dataSourceName);
try
{
dataSource.TestConnection();
var trans = await ((IRootDataSource)dataSource).BeginTransactionAsync();
await trans.DisposeAsync().ConfigureAwait(false);
}
finally
{
Release(dataSource);
}
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ public interface IRootDataSource
/// <param name="transaction">The transaction to wrap.</param>
/// <returns></returns>
IOpenDataSource CreateOpenDataSource(IDbConnection connection, IDbTransaction? transaction = null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ namespace Tortuga.Chain.DataSources;
/// <remarks>
/// This interface is primarily for testing purposes.
/// </remarks>
public interface ITransactionalDataSource : IOpenDataSource
public interface ITransactionalDataSource : IOpenDataSource, IDisposable
#if NET6_0_OR_GREATER
, IAsyncDisposable
#endif

{
/// <summary>
/// Commits this transaction.
/// </summary>
void Commit();
}
}

0 comments on commit d0fcf6a

Please sign in to comment.