Skip to content

Commit

Permalink
Merge pull request #183 from AntonioFalcao/feature/code-smell
Browse files Browse the repository at this point in the history
Code Smell
  • Loading branch information
AntonioFalcaoJr authored Apr 6, 2021
2 parents e216454 + 30f7a66 commit f8d5bcd
Showing 1 changed file with 13 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ namespace Dotnet6.GraphQL4.Repositories.Abstractions.Transactions
{
public class TransactionScopeExecutor<T>
{
private TransactionScopeOption _scopeOption;
private readonly Func<T> _operation;
private readonly Func<CancellationToken, Task<T>> _operationAsync;
private readonly TransactionOptions _transactionOptions = new();

private TransactionScopeAsyncFlowOption _asyncFlowOption;

private Func<bool> _condition;
private Func<CancellationToken, Task<bool>> _conditionAsync;

private readonly TransactionOptions _transactionOptions;
private readonly Func<T> _operation;
private readonly Func<CancellationToken, Task<T>> _operationAsync;
private TransactionScopeOption _scopeOption;

public TransactionScopeExecutor() { }

public TransactionScopeExecutor(Func<T> operation)
{
_operation = operation;
Expand Down Expand Up @@ -52,7 +51,7 @@ public TransactionScopeExecutor<T> WithCondition(Func<bool> condition)
_condition = condition;
return this;
}

public TransactionScopeExecutor<T> WithConditionAsync(Func<CancellationToken, Task<bool>> conditionAsync)
{
_conditionAsync = conditionAsync;
Expand All @@ -68,42 +67,20 @@ public T Execute(Func<T> operation)
}

public T Execute()
{
using var scope = CreateScope();
var result = _operation();

if (_condition())
scope.Complete();

return result;
}
=> Execute(_operation);

public Task<T> ExecuteAsync(CancellationToken cancellationToken)
=> ExecuteAsync(_operationAsync, cancellationToken);

public async Task<T> ExecuteAsync(CancellationToken cancellationToken)
{
using var scope = CreateScope();
var result = await _operationAsync(cancellationToken);

if (await _conditionAsync(cancellationToken))
scope.Complete();

return result;
}

public async Task<T> ExecuteAsync(Func<CancellationToken, Task<T>> 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);
}
}

0 comments on commit f8d5bcd

Please sign in to comment.