forked from OrchardCMS/OrchardCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IAsyncConfigureOptions (OrchardCMS#14759)
- Loading branch information
Showing
6 changed files
with
83 additions
and
17 deletions.
There are no files selected for viewing
4 changes: 3 additions & 1 deletion
4
src/OrchardCore.Modules/OrchardCore.DataProtection.Azure/BlobOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/OrchardCore/OrchardCore.Abstractions/Shell/Builders/Extensions/IAsyncConfigureOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Extensions.Options; | ||
|
||
/// <summary> | ||
/// Used to configure asynchronously a type of options just after a tenant container is created. | ||
/// </summary> | ||
public interface IAsyncConfigureOptions<TOptions> where TOptions : class, IAsyncOptions | ||
{ | ||
/// <summary> | ||
/// Configures asynchronously an options instance. | ||
/// </summary> | ||
ValueTask ConfigureAsync(TOptions options); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/OrchardCore/OrchardCore.Abstractions/Shell/Builders/Extensions/IAsyncOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Microsoft.Extensions.Options; | ||
|
||
/// <summary> | ||
/// Marks a type of options intended to be registered as a singleton and configured asynchronously | ||
/// by an <see cref="IAsyncConfigureOptions{TOptions}"/> just after a tenant container is created. | ||
/// </summary> | ||
public interface IAsyncOptions | ||
{ | ||
} |
55 changes: 52 additions & 3 deletions
55
...ardCore/OrchardCore.Abstractions/Shell/Builders/Extensions/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,63 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Options; | ||
using OrchardCore.Environment.Shell.Builders; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Registers a delegate to be invoked asynchronously after a tenant container is created. | ||
/// Registers a delegate to be invoked asynchronously just after a tenant container is created. | ||
/// </summary> | ||
public static IServiceCollection Initialize(this IServiceCollection services, Func<IServiceProvider, ValueTask> _initializeAsync) | ||
=> services.Configure<ShellContainerOptions>(options => options.Initializers.Add(_initializeAsync)); | ||
public static IServiceCollection Initialize(this IServiceCollection services, Func<IServiceProvider, ValueTask> initializeAsync) | ||
=> services.Configure<ShellContainerOptions>(options => options.Initializers.Add(initializeAsync)); | ||
|
||
/// <summary> | ||
/// Registers a delegate used to configure asynchronously a type of options just after a tenant container is created. | ||
/// </summary> | ||
public static IServiceCollection Configure<TOptions>( | ||
this IServiceCollection services, Func<IServiceProvider, TOptions, ValueTask> configureAsync) | ||
where TOptions : class, IAsyncOptions, new() | ||
{ | ||
if (!services.Any(d => d.ServiceType == typeof(TOptions))) | ||
{ | ||
services.AddSingleton(new TOptions()); | ||
} | ||
|
||
services.Initialize(async sp => | ||
{ | ||
var options = sp.GetRequiredService<TOptions>(); | ||
await configureAsync(sp, options); | ||
}); | ||
|
||
return services; | ||
} | ||
|
||
/// <summary> | ||
/// Registers an <see cref="IAsyncConfigureOptions{TOptions}"/> used to configure | ||
/// asynchronously a type of options just after a tenant container is created. | ||
/// </summary> | ||
public static IServiceCollection Configure<TOptions, TConfigure>(this IServiceCollection services) | ||
where TOptions : class, IAsyncOptions, new() where TConfigure : IAsyncConfigureOptions<TOptions> | ||
{ | ||
if (!services.Any(d => d.ServiceType == typeof(TOptions))) | ||
{ | ||
services.AddSingleton(new TOptions()); | ||
} | ||
|
||
if (!services.Any(d => d.ServiceType == typeof(TConfigure))) | ||
{ | ||
services.AddTransient(typeof(TConfigure)); | ||
services.Initialize(async sp => | ||
{ | ||
var options = sp.GetRequiredService<TOptions>(); | ||
var setup = sp.GetRequiredService<TConfigure>(); | ||
await setup.ConfigureAsync(options); | ||
}); | ||
} | ||
|
||
return services; | ||
} | ||
} |