-
Notifications
You must be signed in to change notification settings - Fork 0
v1.0.15 #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v1.0.15 #34
Changes from all commits
9efca9b
381915e
d0a285b
f042bc8
428357e
aadff7b
26117a2
a9506d3
d8e5322
8a9b0cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using BBT.Aether.Uow; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace BBT.Aether.DependencyInjection; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for IServiceScopeFactory. | ||
| /// </summary> | ||
| public static class ServiceScopeFactoryExtensions | ||
| { | ||
| /// <summary> | ||
| /// Executes the given action within a new dependency injection scope and a new unit of work. | ||
| /// Manages ambient service provider propagation. | ||
| /// </summary> | ||
| /// <param name="scopeFactory">The service scope factory.</param> | ||
| /// <param name="action">The action to execute, receiving the scoped service provider.</param> | ||
| /// <param name="options">Unit of work options. Defaults to a new UoW with default settings.</param> | ||
| /// <param name="cancellationToken">Cancellation token.</param> | ||
| /// <returns>A task representing the asynchronous operation.</returns> | ||
| public static async Task ExecuteInNewUnitOfWorkScopeAsync( | ||
| this IServiceScopeFactory scopeFactory, | ||
| Func<IServiceProvider, Task> action, | ||
| UnitOfWorkOptions? options = null, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| await using var scope = scopeFactory.CreateAsyncScope(); | ||
| var sp = scope.ServiceProvider; | ||
|
|
||
| // Propagate ambient service provider for the new scope | ||
| var previousAmbient = AmbientServiceProvider.Current; | ||
| AmbientServiceProvider.Current = sp; | ||
|
|
||
| try | ||
| { | ||
| var uowManager = sp.GetRequiredService<IUnitOfWorkManager>(); | ||
|
|
||
| // Begin UnitOfWork | ||
| await using var uow = await uowManager.BeginAsync(options, cancellationToken); | ||
|
|
||
| // Execute action | ||
| await action(sp); | ||
|
|
||
| // Commit UnitOfWork | ||
| await uow.CommitAsync(cancellationToken); | ||
| } | ||
| finally | ||
| { | ||
| // Restore previous ambient context | ||
| AmbientServiceProvider.Current = previousAmbient; | ||
| } | ||
| } | ||
|
Comment on lines
+23
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method duplicates the logic for creating a scope and managing the public static Task ExecuteInNewUnitOfWorkScopeAsync(
this IServiceScopeFactory scopeFactory,
Func<IServiceProvider, Task> action,
UnitOfWorkOptions? options = null,
CancellationToken cancellationToken = default)
{
return scopeFactory.ExecuteInNewScopeAsync(async sp =>
{
var uowManager = sp.GetRequiredService<IUnitOfWorkManager>();
// Begin UnitOfWork
await using var uow = await uowManager.BeginAsync(options, cancellationToken);
// Execute action
await action(sp);
// Commit UnitOfWork
await uow.CommitAsync(cancellationToken);
});
} |
||
|
|
||
| /// <summary> | ||
| /// Executes the given function within a new dependency injection scope and a new unit of work, returning a result. | ||
| /// Manages ambient service provider propagation. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of the result.</typeparam> | ||
| /// <param name="scopeFactory">The service scope factory.</param> | ||
| /// <param name="func">The function to execute, receiving the scoped service provider.</param> | ||
| /// <param name="options">Unit of work options. Defaults to a new UoW with default settings.</param> | ||
| /// <param name="cancellationToken">Cancellation token.</param> | ||
| /// <returns>The result of the function execution.</returns> | ||
| public static async Task<T> ExecuteInNewUnitOfWorkScopeAsync<T>( | ||
| this IServiceScopeFactory scopeFactory, | ||
| Func<IServiceProvider, Task<T>> func, | ||
| UnitOfWorkOptions? options = null, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| await using var scope = scopeFactory.CreateAsyncScope(); | ||
| var sp = scope.ServiceProvider; | ||
|
|
||
| // Propagate ambient service provider for the new scope | ||
| var previousAmbient = AmbientServiceProvider.Current; | ||
| AmbientServiceProvider.Current = sp; | ||
|
|
||
| try | ||
| { | ||
| var uowManager = sp.GetRequiredService<IUnitOfWorkManager>(); | ||
|
|
||
| // Begin UnitOfWork | ||
| await using var uow = await uowManager.BeginAsync(options, cancellationToken); | ||
|
|
||
| // Execute function | ||
| var result = await func(sp); | ||
|
|
||
| // Commit UnitOfWork | ||
| await uow.CommitAsync(cancellationToken); | ||
|
|
||
| return result; | ||
| } | ||
| finally | ||
| { | ||
| // Restore previous ambient context | ||
| AmbientServiceProvider.Current = previousAmbient; | ||
| } | ||
| } | ||
|
Comment on lines
+66
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the non-generic version, this method duplicates the scope creation and public static Task<T> ExecuteInNewUnitOfWorkScopeAsync<T>(
this IServiceScopeFactory scopeFactory,
Func<IServiceProvider, Task<T>> func,
UnitOfWorkOptions? options = null,
CancellationToken cancellationToken = default)
{
return scopeFactory.ExecuteInNewScopeAsync(async sp =>
{
var uowManager = sp.GetRequiredService<IUnitOfWorkManager>();
// Begin UnitOfWork
await using var uow = await uowManager.BeginAsync(options, cancellationToken);
// Execute function
var result = await func(sp);
// Commit UnitOfWork
await uow.CommitAsync(cancellationToken);
return result;
});
} |
||
|
|
||
| /// <summary> | ||
| /// Executes the given action within a new dependency injection scope without starting a unit of work. | ||
| /// Manages ambient service provider propagation. | ||
| /// </summary> | ||
| /// <param name="scopeFactory">The service scope factory.</param> | ||
| /// <param name="action">The action to execute, receiving the scoped service provider.</param> | ||
| /// <returns>A task representing the asynchronous operation.</returns> | ||
| public static async Task ExecuteInNewScopeAsync( | ||
| this IServiceScopeFactory scopeFactory, | ||
| Func<IServiceProvider, Task> action) | ||
| { | ||
| await using var scope = scopeFactory.CreateAsyncScope(); | ||
| var sp = scope.ServiceProvider; | ||
|
|
||
| // Propagate ambient service provider for the new scope | ||
| var previousAmbient = AmbientServiceProvider.Current; | ||
| AmbientServiceProvider.Current = sp; | ||
|
|
||
| try | ||
| { | ||
| await action(sp); | ||
| } | ||
| finally | ||
| { | ||
| // Restore previous ambient context | ||
| AmbientServiceProvider.Current = previousAmbient; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Executes the given function within a new dependency injection scope without starting a unit of work, returning a result. | ||
| /// Manages ambient service provider propagation. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of the result.</typeparam> | ||
| /// <param name="scopeFactory">The service scope factory.</param> | ||
| /// <param name="func">The function to execute, receiving the scoped service provider.</param> | ||
| /// <returns>The result of the function execution.</returns> | ||
| public static async Task<T> ExecuteInNewScopeAsync<T>( | ||
| this IServiceScopeFactory scopeFactory, | ||
| Func<IServiceProvider, Task<T>> func) | ||
| { | ||
| await using var scope = scopeFactory.CreateAsyncScope(); | ||
| var sp = scope.ServiceProvider; | ||
|
|
||
| // Propagate ambient service provider for the new scope | ||
| var previousAmbient = AmbientServiceProvider.Current; | ||
| AmbientServiceProvider.Current = sp; | ||
|
|
||
| try | ||
| { | ||
| return await func(sp); | ||
| } | ||
| finally | ||
| { | ||
| // Restore previous ambient context | ||
| AmbientServiceProvider.Current = previousAmbient; | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): There is an extra closing brace at the end of the file which will cause a compile error.
The class and namespace are already closed, so the final
}leaves the file with unbalanced braces. Remove that last brace at the end of the file.