Skip to content

Commit

Permalink
Merge pull request #9 from codibre/refactor-sqlconnection-3
Browse files Browse the repository at this point in the history
fix: refactoring AsyncConnection again
  • Loading branch information
Farenheith authored Jul 22, 2024
2 parents 96c6738 + 4a100b2 commit d319bb3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[![Actions Status](https://github.com/Codibre/dotnet-async-local-mssql-session/workflows/build/badge.svg)](https://github.com/Codibre/dotnet-async-local-mssql-session/actions)
[![Actions Status](https://github.com/Codibre/dotnet-async-local-mssql-session/workflows/test/badge.svg)](https://github.com/Codibre/dotnet-async-local-mssql-session/actions)
[![Actions Status](https://github.com/Codibre/dotnet-async-local-mssql-session/workflows/lint/badge.svg)](https://github.com/Codibre/dotnet-async-local-mssql-session/actions)
[![Test Coverage](https://api.codeclimate.com/v1/badges/a70dd4bf03f42c1f05b0/test_coverage)](https://codeclimate.com/github/codibre/dotnet-async-local-mssql-session/test_coverage)
[![Maintainability](https://api.codeclimate.com/v1/badges/a70dd4bf03f42c1f05b0/maintainability)](https://codeclimate.com/github/codibre/dotnet-async-local-mssql-session/maintainability)

# Codibre.MSSqlSession

Library for SqlClient connections using AsyncLocal for management
Expand Down
12 changes: 5 additions & 7 deletions src/Codibre.MSSqlSession/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,24 @@ int maxConcurrency
await foreach (var x in enumerable)
{
await semaphore.WaitAsync();
RunSemaphoredTransform(transform, semaphore, result, exceptions, x);
RunSemaphoredTransform(async () => result.Add(await transform(x)), semaphore, exceptions);
}
await semaphore.WaitAll(maxConcurrency);
if (exceptions.Count > 0) throw new AggregateException(exceptions);
return result;
}

private static void RunSemaphoredTransform<T, R>(
Func<T, Task<R>> transform,
private static void RunSemaphoredTransform(
Func<Task> call,
SemaphoreSlim semaphore,
List<R> result,
List<Exception> exceptions,
T item
List<Exception> exceptions
)
{
_ = Task.Run(async () =>
{
try
{
result.Add(await transform(item));
await call();
}
catch (Exception ex)
{
Expand Down
18 changes: 9 additions & 9 deletions src/Codibre.MSSqlSession/Impl/BatchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,20 @@ public Task RunInTransaction(Action query, RunInTransactionOptions? options = nu
return new ValueTask();
}, options);

public async Task<T> RunInTransaction<T>(Func<ValueTask<T>> query, RunInTransactionOptions? options = null)
=> await RunInTransaction((_) => query(), options);
public Task<T> RunInTransaction<T>(Func<ValueTask<T>> query, RunInTransactionOptions? options = null)
=> RunInTransaction((_) => query(), options);

public async Task<T> RunInTransaction<T>(Func<IBatchQuery, ValueTask<T>> query, RunInTransactionOptions? options = null)
{
T? result = default;
if (options is null) await RunInTransaction(async (bq) =>
{
result = await query(bq);
});
{
result = await query(bq);
});
else await RunInTransaction(async (bq) =>
{
result = await query(bq);
}, options);
{
result = await query(bq);
}, options);
return result!;
}

Expand All @@ -161,7 +161,7 @@ public async Task RunInTransaction(Func<IBatchQuery, ValueTask> query, RunInTran
}
catch (Exception)
{
if (_transactionControl.Open) await _session.Rollback();
if (_transactionControl.Open) await RollBack();
throw;
}
}
Expand Down

0 comments on commit d319bb3

Please sign in to comment.