-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
[API Proposal]: Add extensions to create an OptionsBuilder with ValidateOnStart support #89263
Comments
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection Issue DetailsBackground and motivationBased on cloud native scenarios, these extensions methods were used and they make sense to add to the System.Microsoft.Extensions.DependencyInjection library. Note that in cloud native, these methods were called "AddValidatedOptions"; here they are called "AddValidateOnStartOptions". API Proposalnamespace namespace Microsoft.Extensions.DependencyInjection;
public static partial class OptionsBuilderExtensions
{
// The one existing extension method:
public static OptionsBuilder<TOptions> ValidateOnStart
<[DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.
PublicParameterlessConstructor)] TOptions>
(this OptionsBuilder<TOptions> optionsBuilder)
where TOptions : class;
// New one to call ValidateOnStart() above
+ public static OptionsBuilder<TOptions> AddValidateOnStartOptions
+ <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TOptions>
+ (this IServiceCollection services,
+ string? name = null)
+ where TOptions : class
{
// Simple wrapper like this:
return new OptionsBuilder<TOptions>(services, name ?? Options.DefaultName).ValidateOnStart();
}
// New one to support IValidateOptions and call ValidateOnStart() above
+ public static OptionsBuilder<TOptions> AddValidateOnStartOptions<
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TOptions,
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TValidateOptions>(
+ this IServiceCollection services,
+ string? name = null)
+ where TOptions : class
+ where TValidateOptions : class, IValidateOptions<TOptions>
{
// Simple wrapper like this:
services.AddOptions().TryAddEnumerable(ServiceDescriptor.Singleton<IValidateOptions<TOptions>, TValidateOptions>());
return new OptionsBuilder<TOptions>(services, name ?? Options.DefaultName).ValidateOnStart();
} API Usage// Before:
IHostBuilder builder = ...
builder.services
.AddOptions<MyOptions>()
.Bind(builder.Configuration.GetSection(MyConfigOptions.MyConfig))
.Validate(o => o.MySetting == true)
.ValidateOnStart();
// After:
IHostBuilder builder = ...
builder.Services
.AddValidateOnStartOptions<MyOptions>()
.Bind(builder.Configuration.GetSection(MyConfigOptions.MyConfig))
.Validate(o => o.MySetting == true);
// Before:
IHostBuilder builder = ...
builder.Services
.AddOptions<MyOptions>()
.AddSingleton<IValidateOptions<MyOptions>>(new MyOptionsValidator(builder .Name)) // or TryAddEnumerable
.Bind(builder.Configuration.GetSection(MyConfigOptions.MyConfig))
.ValidateOnStart();
// After:
IHostBuilder builder = ...
builder.Services.
.AddValidateOnStartOptions<MyOptions, MyOptionsValidator>(builder.Name))
.Bind(builder.Configuration.GetSection(MyConfigOptions.MyConfig)) Alternative DesignsNo response RisksNo response
|
@steveharter you mention
But I think we want to add to |
Why does the proposal have |
namespace namespace Microsoft.Extensions.DependencyInjection;
public static partial class OptionsBuilderExtensions
{
// The one existing extension method:
// public static OptionsBuilder<TOptions> ValidateOnStart
// <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TOptions>
// (this OptionsBuilder<TOptions> optionsBuilder)
// where TOptions : class;
// New one to call ValidateOnStart() above
public static OptionsBuilder<TOptions> AddOptionsWithValidateOnStart
<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TOptions>
(this IServiceCollection services,
string? name = null)
where TOptions : class;
// New one to support IValidateOptions and call ValidateOnStart() above
public static OptionsBuilder<TOptions> AddOptionsWithValidateOnStart<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TOptions,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TValidateOptions>(
this IServiceCollection services,
string? name = null)
where TOptions : class
where TValidateOptions : class, IValidateOptions<TOptions>;
} |
@steveharter I just noticed that this proposal put the new |
Background and motivation
Based on cloud native scenarios, these extensions methods were used and they make sense to add to the System.Microsoft.Extensions.Options.library.
Note that in cloud native, these methods were called "AddValidatedOptions"; here they are called "AddValidateOnStartOptions".
API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: