diff --git a/src/Dotnet6.GraphQL4.Repositories.Abstractions/Transactions/TransactionScopeExecutor.cs b/src/Dotnet6.GraphQL4.Repositories.Abstractions/Transactions/TransactionScopeExecutor.cs index 5d3fa43a..73cdf2b5 100644 --- a/src/Dotnet6.GraphQL4.Repositories.Abstractions/Transactions/TransactionScopeExecutor.cs +++ b/src/Dotnet6.GraphQL4.Repositories.Abstractions/Transactions/TransactionScopeExecutor.cs @@ -7,18 +7,17 @@ namespace Dotnet6.GraphQL4.Repositories.Abstractions.Transactions { public class TransactionScopeExecutor { - private TransactionScopeOption _scopeOption; + private readonly Func _operation; + private readonly Func> _operationAsync; + private readonly TransactionOptions _transactionOptions = new(); + private TransactionScopeAsyncFlowOption _asyncFlowOption; - private Func _condition; private Func> _conditionAsync; - - private readonly TransactionOptions _transactionOptions; - private readonly Func _operation; - private readonly Func> _operationAsync; + private TransactionScopeOption _scopeOption; public TransactionScopeExecutor() { } - + public TransactionScopeExecutor(Func operation) { _operation = operation; @@ -52,7 +51,7 @@ public TransactionScopeExecutor WithCondition(Func condition) _condition = condition; return this; } - + public TransactionScopeExecutor WithConditionAsync(Func> conditionAsync) { _conditionAsync = conditionAsync; @@ -68,42 +67,20 @@ public T Execute(Func operation) } public T Execute() - { - using var scope = CreateScope(); - var result = _operation(); - - if (_condition()) - scope.Complete(); - - return result; - } + => Execute(_operation); + + public Task ExecuteAsync(CancellationToken cancellationToken) + => ExecuteAsync(_operationAsync, cancellationToken); - public async Task ExecuteAsync(CancellationToken cancellationToken) - { - using var scope = CreateScope(); - var result = await _operationAsync(cancellationToken); - - if (await _conditionAsync(cancellationToken)) - scope.Complete(); - - return result; - } - public async Task ExecuteAsync(Func> operationAsync, CancellationToken cancellationToken) { using var scope = CreateScope(); var result = await operationAsync(cancellationToken); - - if (await _conditionAsync(cancellationToken)) - scope.Complete(); - + if (await _conditionAsync(cancellationToken)) scope.Complete(); return result; } private TransactionScope CreateScope() - => new( - scopeOption: _scopeOption, - transactionOptions: _transactionOptions, - asyncFlowOption: _asyncFlowOption); + => new(_scopeOption, _transactionOptions, _asyncFlowOption); } } \ No newline at end of file