-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTransactionAwareRequestHandlerDecorator.cs
36 lines (29 loc) · 1.45 KB
/
TransactionAwareRequestHandlerDecorator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System.Transactions;
namespace softaware.Cqs.Decorators.Transaction;
/// <summary>
/// A decorator for creating a <see cref="TransactionScope"/> for the request handler.
/// The transaction gets committed when the decorated handler successfully executes.
/// </summary>
/// <typeparam name="TRequest">The type of the request to execute.</typeparam>
/// <typeparam name="TResult">The type of the result.</typeparam>
public class TransactionAwareRequestHandlerDecorator<TRequest, TResult> : IRequestHandler<TRequest, TResult>
where TRequest : IRequest<TResult>
{
private readonly IRequestHandler<TRequest, TResult> decoratee;
public TransactionAwareRequestHandlerDecorator(IRequestHandler<TRequest, TResult> decoratee) =>
this.decoratee = decoratee ?? throw new ArgumentNullException(nameof(decoratee));
public Task<TResult> HandleAsync(TRequest query) => this.HandleAsync(query, default);
public async Task<TResult> HandleAsync(TRequest query, CancellationToken cancellationToken)
{
TransactionOptions transactionOptions = new TransactionOptions
{
IsolationLevel = IsolationLevel.ReadCommitted
};
using (var tx = new TransactionScope(TransactionScopeOption.Required, transactionOptions, TransactionScopeAsyncFlowOption.Enabled))
{
var result = await this.decoratee.HandleAsync(query, cancellationToken);
tx.Complete();
return result;
}
}
}