Skip to content

Commit

Permalink
feature(#59): Added SuppressTransaction to TransactionScopeHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
jhartmann123 authored and davidroth committed Sep 6, 2024
1 parent ccc6439 commit 84b936c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/Common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
- [Common](#common)
- [PropertyUtil](#propertyutil)
- [PathUtil](#pathutil)
- [TempFileStream](#tempfilestream)
- [TransactionScopeHandler](#transactionscopehandler)

This project contains general, framework independent utilities and abstractions.

Expand Down Expand Up @@ -43,4 +45,27 @@ await using (var fs = new TempFileStream())
// Upload file
}
// File was deleted at this point.
```

## TransactionScopeHandler

The `TransactionScopeHandler` provides an easy way to run code within a transaction. The transaction optios are
```cs
IsolationLevel = IsolationLevel.ReadCommitted
Timeout = TransactionManager.MaximumTimeout
```

Example:
```cs
// Runs async method FooMethod in a transaction. An ambient transaction gets used if it exists, otherwise a new one will be created.
await transactionScopeHandler.RunInTransactionScope(FooMethod)

// Runs async method BarMethod in a new transaction.
await transactionScopeHandler.RunInTransactionScope(async () => { await BarMethod(); return 42; }, TransactionScopeOptions.RequiresNew);

// Runs LogMethod and supresses any ambient transaction.
await transactionScopeHandler.RunInTransactionScope(LogMethod, TransactionScopeOptions.Suppress);

// Shortcut for supressing the transaction
await transactionScopeHandler.SuppressTransaction(LogMethod);
```
15 changes: 15 additions & 0 deletions src/Common/src/Transactions/TransactionScopeHandlerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Fusonic GmbH. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license information.

using System.Transactions;

namespace Fusonic.Extensions.Common.Transactions;

public static class TransactionScopeHandlerExtensions
{
public static async Task<T> SuppressTransaction<T>(this ITransactionScopeHandler transactionScopeHandler, Func<Task<T>> action)
=> await transactionScopeHandler.RunInTransactionScope(action, TransactionScopeOption.Suppress);

public static async Task SuppressTransaction(this ITransactionScopeHandler transactionScopeHandler, Func<Task> action)
=> await transactionScopeHandler.RunInTransactionScope(action, TransactionScopeOption.Suppress);
}

0 comments on commit 84b936c

Please sign in to comment.