-
Notifications
You must be signed in to change notification settings - Fork 22
feat: Add DI for multi provider #621
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a42ea11
feat: Implement multi-provider configuration with dependency injectio…
askpt 148484e
feat: Add multi-provider support with dependency injection and flag e…
askpt 411a491
refactor: Simplify AddMultiProvider method by removing redundant para…
askpt 49102d0
test: Add unit tests for MultiProviderBuilder and MultiProviderDepend…
askpt 73679e1
fix: Update project reference to use OpenFeature.Hosting instead of O…
askpt 4e3bcbb
refactor: Remove MultiProviderOptions class as part of code cleanup
askpt 3d068d0
docs: add dependency injection documentation to MultiProvider README
askpt 1b2ef7c
Apply suggestions from code review
askpt 3c1eb53
Apply suggestions from code review
askpt 2656177
fix: Improve null argument exception messages in MultiProvider config…
askpt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
94 changes: 94 additions & 0 deletions
94
src/OpenFeature.Providers.MultiProvider/DependencyInjection/FeatureBuilderExtensions.cs
This file contains hidden or 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,94 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using OpenFeature.Hosting; | ||
|
|
||
| namespace OpenFeature.Providers.MultiProvider.DependencyInjection; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for configuring the multi-provider with <see cref="OpenFeatureBuilder"/>. | ||
| /// </summary> | ||
| public static class FeatureBuilderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds a multi-provider to the <see cref="OpenFeatureBuilder"/> with a configuration builder. | ||
| /// </summary> | ||
| /// <param name="builder">The <see cref="OpenFeatureBuilder"/> instance to configure.</param> | ||
| /// <param name="configure"> | ||
| /// A delegate to configure the multi-provider using the <see cref="MultiProviderBuilder"/>. | ||
| /// </param> | ||
| /// <returns>The <see cref="OpenFeatureBuilder"/> instance for chaining.</returns> | ||
| public static OpenFeatureBuilder AddMultiProvider( | ||
| this OpenFeatureBuilder builder, | ||
| Action<MultiProviderBuilder> configure) | ||
| { | ||
| if (builder == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(builder)); | ||
| } | ||
|
|
||
| if (configure == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(configure)); | ||
| } | ||
|
|
||
| return builder.AddProvider( | ||
| serviceProvider => CreateMultiProviderFromConfigure(serviceProvider, configure)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a multi-provider with a specific domain to the <see cref="OpenFeatureBuilder"/> with a configuration builder. | ||
| /// </summary> | ||
| /// <param name="builder">The <see cref="OpenFeatureBuilder"/> instance to configure.</param> | ||
| /// <param name="domain">The unique domain of the provider.</param> | ||
| /// <param name="configure"> | ||
| /// A delegate to configure the multi-provider using the <see cref="MultiProviderBuilder"/>. | ||
| /// </param> | ||
| /// <returns>The <see cref="OpenFeatureBuilder"/> instance for chaining.</returns> | ||
| public static OpenFeatureBuilder AddMultiProvider( | ||
| this OpenFeatureBuilder builder, | ||
| string domain, | ||
| Action<MultiProviderBuilder> configure) | ||
| { | ||
| if (builder == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(builder)); | ||
| } | ||
|
|
||
| if (string.IsNullOrWhiteSpace(domain)) | ||
| { | ||
| throw new ArgumentException("Domain cannot be null or empty.", nameof(domain)); | ||
| } | ||
|
|
||
| if (configure == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(configure), "Configure action cannot be null. Please provide a valid configuration for the multi-provider."); | ||
| } | ||
|
|
||
| return builder.AddProvider( | ||
| domain, | ||
| (serviceProvider, _) => CreateMultiProviderFromConfigure(serviceProvider, configure)); | ||
kylejuliandev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private static MultiProvider CreateMultiProviderFromConfigure(IServiceProvider serviceProvider, Action<MultiProviderBuilder> configure) | ||
| { | ||
| // Build the multi-provider configuration using the builder | ||
| var multiProviderBuilder = new MultiProviderBuilder(); | ||
|
|
||
| // Apply the configuration action | ||
| configure(multiProviderBuilder); | ||
|
|
||
| // Build provider entries and strategy from the builder using the service provider | ||
| var providerEntries = multiProviderBuilder.BuildProviderEntries(serviceProvider); | ||
| var evaluationStrategy = multiProviderBuilder.BuildEvaluationStrategy(serviceProvider); | ||
|
|
||
| if (providerEntries.Count == 0) | ||
| { | ||
| throw new InvalidOperationException("At least one provider must be configured for the multi-provider."); | ||
| } | ||
|
|
||
| // Get logger from DI | ||
| var logger = serviceProvider.GetService<ILogger<MultiProvider>>(); | ||
|
|
||
| return new MultiProvider(providerEntries, evaluationStrategy, logger); | ||
| } | ||
| } | ||
135 changes: 135 additions & 0 deletions
135
src/OpenFeature.Providers.MultiProvider/DependencyInjection/MultiProviderBuilder.cs
This file contains hidden or 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,135 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using OpenFeature.Providers.MultiProvider.Models; | ||
| using OpenFeature.Providers.MultiProvider.Strategies; | ||
|
|
||
| namespace OpenFeature.Providers.MultiProvider.DependencyInjection; | ||
|
|
||
| /// <summary> | ||
| /// Builder for configuring a multi-provider with dependency injection. | ||
| /// </summary> | ||
| public class MultiProviderBuilder | ||
| { | ||
| private readonly List<Func<IServiceProvider, ProviderEntry>> _providerFactories = []; | ||
| private Func<IServiceProvider, BaseEvaluationStrategy>? _strategyFactory; | ||
|
|
||
| /// <summary> | ||
| /// Adds a provider to the multi-provider configuration using a factory method. | ||
| /// </summary> | ||
| /// <param name="name">The name for the provider.</param> | ||
| /// <param name="factory">A factory method to create the provider instance.</param> | ||
| /// <returns>The <see cref="MultiProviderBuilder"/> instance for chaining.</returns> | ||
| public MultiProviderBuilder AddProvider(string name, Func<IServiceProvider, FeatureProvider> factory) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(name)) | ||
| { | ||
| throw new ArgumentException("Provider name cannot be null or empty.", nameof(name)); | ||
| } | ||
|
|
||
| if (factory == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(factory), "Provider configuration cannot be null."); | ||
| } | ||
|
|
||
| return AddProvider<FeatureProvider>(name, sp => factory(sp)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a provider to the multi-provider configuration using a type. | ||
| /// </summary> | ||
| /// <typeparam name="TProvider">The type of the provider to add.</typeparam> | ||
| /// <param name="name">The name for the provider.</param> | ||
| /// <param name="factory">An optional factory method to create the provider instance. If not provided, the provider will be resolved from the service provider.</param> | ||
| /// <returns>The <see cref="MultiProviderBuilder"/> instance for chaining.</returns> | ||
| public MultiProviderBuilder AddProvider<TProvider>(string name, Func<IServiceProvider, TProvider>? factory = null) | ||
| where TProvider : FeatureProvider | ||
| { | ||
| if (string.IsNullOrWhiteSpace(name)) | ||
| { | ||
| throw new ArgumentException("Provider name cannot be null or empty.", nameof(name)); | ||
| } | ||
|
|
||
| this._providerFactories.Add(sp => | ||
| { | ||
| var provider = factory != null | ||
| ? factory(sp) | ||
| : sp.GetRequiredService<TProvider>(); | ||
| return new ProviderEntry(provider, name); | ||
| }); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a provider instance to the multi-provider configuration. | ||
| /// </summary> | ||
| /// <param name="name">The name for the provider.</param> | ||
| /// <param name="provider">The provider instance to add.</param> | ||
| /// <returns>The <see cref="MultiProviderBuilder"/> instance for chaining.</returns> | ||
| public MultiProviderBuilder AddProvider(string name, FeatureProvider provider) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(name)) | ||
| { | ||
| throw new ArgumentException("Provider name cannot be null or empty.", nameof(name)); | ||
| } | ||
|
|
||
| if (provider == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(provider), "Provider configuration cannot be null."); | ||
| } | ||
|
|
||
| return AddProvider<FeatureProvider>(name, _ => provider); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the evaluation strategy for the multi-provider. | ||
| /// </summary> | ||
| /// <typeparam name="TStrategy">The type of the evaluation strategy.</typeparam> | ||
| /// <returns>The <see cref="MultiProviderBuilder"/> instance for chaining.</returns> | ||
| public MultiProviderBuilder UseStrategy<TStrategy>() | ||
| where TStrategy : BaseEvaluationStrategy, new() | ||
| { | ||
| return UseStrategy(static _ => new TStrategy()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the evaluation strategy for the multi-provider using a factory method. | ||
| /// </summary> | ||
| /// <param name="factory">A factory method to create the strategy instance.</param> | ||
| /// <returns>The <see cref="MultiProviderBuilder"/> instance for chaining.</returns> | ||
| public MultiProviderBuilder UseStrategy(Func<IServiceProvider, BaseEvaluationStrategy> factory) | ||
| { | ||
| this._strategyFactory = factory ?? throw new ArgumentNullException(nameof(factory), "Strategy for multi-provider cannot be null."); | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the evaluation strategy for the multi-provider. | ||
| /// </summary> | ||
| /// <param name="strategy">The strategy instance to use.</param> | ||
| /// <returns>The <see cref="MultiProviderBuilder"/> instance for chaining.</returns> | ||
| public MultiProviderBuilder UseStrategy(BaseEvaluationStrategy strategy) | ||
| { | ||
| if (strategy == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(strategy)); | ||
| } | ||
|
|
||
| return UseStrategy(_ => strategy); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Builds the provider entries using the service provider. | ||
| /// </summary> | ||
| internal List<ProviderEntry> BuildProviderEntries(IServiceProvider serviceProvider) | ||
| { | ||
| return this._providerFactories.Select(factory => factory(serviceProvider)).ToList(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Builds the evaluation strategy using the service provider. | ||
| /// </summary> | ||
| internal BaseEvaluationStrategy? BuildEvaluationStrategy(IServiceProvider serviceProvider) | ||
| { | ||
| return this._strategyFactory?.Invoke(serviceProvider); | ||
| } | ||
| } |
25 changes: 13 additions & 12 deletions
25
src/OpenFeature.Providers.MultiProvider/OpenFeature.Providers.MultiProvider.csproj
This file contains hidden or 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,17 +1,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <RootNamespace>OpenFeature.Providers.MultiProvider</RootNamespace> | ||
| <PackageReadmeFile>README.md</PackageReadmeFile> | ||
| </PropertyGroup> | ||
| <PropertyGroup> | ||
| <RootNamespace>OpenFeature.Providers.MultiProvider</RootNamespace> | ||
| <PackageReadmeFile>README.md</PackageReadmeFile> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <InternalsVisibleTo Include="DynamicProxyGenAssembly2" /> | ||
| <InternalsVisibleTo Include="OpenFeature.Providers.MultiProvider.Tests" /> | ||
| <None Include="README.md" Pack="true" PackagePath="/" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <InternalsVisibleTo Include="DynamicProxyGenAssembly2" /> | ||
| <InternalsVisibleTo Include="OpenFeature.Providers.MultiProvider.Tests" /> | ||
| <None Include="README.md" Pack="true" PackagePath="/" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\OpenFeature\OpenFeature.csproj" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\OpenFeature\OpenFeature.csproj" /> | ||
| <ProjectReference Include="..\OpenFeature.Hosting\OpenFeature.Hosting.csproj" /> | ||
| </ItemGroup> | ||
| </Project> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.