-
Notifications
You must be signed in to change notification settings - Fork 23
feat: Extract minimal DI integration to OpenFeature.Providers.DependencyInjection #596
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
base: main
Are you sure you want to change the base?
feat: Extract minimal DI integration to OpenFeature.Providers.DependencyInjection #596
Conversation
Introduced a new project, `OpenFeature.DependencyInjection.Abstractions`, to the solution. This project supports dependency injection abstractions and targets multiple frameworks (`netstandard2.0`, `net8.0`, `net9.0`, `net462`) for broad compatibility. Configured the project with the `Microsoft.NET.Sdk` SDK and set the root namespace to `OpenFeature.DependencyInjection.Abstractions`. Added dependencies on `Microsoft.Extensions.DependencyInjection.Abstractions` and `Microsoft.Extensions.Options` to enable DI and options configuration.
Refactored the OpenFeature framework to introduce `OpenFeatureProviderBuilder`, enhancing support for dependency injection and provider management. - Changed namespaces to align with DI abstractions. - Made `FeatureCodes` public for broader accessibility. - Added `InternalsVisibleTo` for testing and project references. - Introduced `OpenFeatureProviderBuilder` for managing providers and policies. - Added extension methods for provider and policy registration. - Refactored `OpenFeatureBuilder` to inherit from `OpenFeatureProviderBuilder`. - Consolidated shared functionality in `OpenFeatureProviderOptions`. - Updated `FeatureBuilderExtensions` and `InMemoryProviderOptions` to use the new abstraction. - Updated tests to reflect new method signatures and hierarchy. - Removed redundant methods and properties, improving code organization. These changes improve maintainability, extensibility, and alignment with modern DI patterns.
Simplified the `AddProvider` API by removing the `TFeatureProvider` generic type parameter and directly using the `FeatureProvider` type. Updated the `TOptions` parameter to ensure it derives from `OpenFeatureProviderOptions`. Added a `<ProjectReference>` to `OpenFeature.csproj` in `OpenFeature.DependencyInjection.Abstractions.csproj`. Updated `OpenFeatureOptions` to `OpenFeatureProviderOptions` in the default name selector policy. Refactored `AddInMemoryProvider` methods to align with the new API. Updated tests in `OpenFeatureBuilderExtensionsTests` to reflect the changes and validate the updated functionality. Performed general code cleanup, including XML documentation updates, to improve clarity and maintain consistency across the codebase.
Refactored `FeatureLifecycleManager` to modularize provider, hook, and handler initialization with new methods: `InitializeProvidersAsync`, `InitializeHooks`, and `InitializeHandlers`. Updated `EnsureInitializedAsync` to use these methods for improved readability and maintainability. Revised `AddInMemoryProvider` in `FeatureBuilderExtensions` to use a generic `FeatureProvider` abstraction. Adjusted `CreateProvider` methods accordingly. Improved code clarity in `FeatureFlagIntegrationTest` by renaming variables for consistency and removing redundant assignments.
Updated FeatureLifecycleManagerTests to replace OpenFeatureOptions with OpenFeatureProviderOptions for configuring feature providers. Added support for hooks and keyed singletons to enhance modularity. Introduced additional feature flag retrieval in FeatureFlagIntegrationTest. Added dependency on OpenFeature.DependencyInjection.Abstractions.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #596 +/- ##
==========================================
- Coverage 89.99% 89.91% -0.09%
==========================================
Files 77 80 +3
Lines 3198 3212 +14
Branches 368 373 +5
==========================================
+ Hits 2878 2888 +10
Misses 251 251
- Partials 69 73 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
src/OpenFeature.Providers.DependencyInjection/OpenFeatureProviderOptions.cs
Show resolved
Hide resolved
src/OpenFeature.Providers.DependencyInjection/Diagnostics/FeatureCodes.cs
Show resolved
Hide resolved
...Feature.DependencyInjection.Abstractions/OpenFeature.DependencyInjection.Abstractions.csproj
Outdated
Show resolved
Hide resolved
src/OpenFeature.Providers.DependencyInjection/OpenFeatureProviderBuilder.cs
Show resolved
Hide resolved
…hub.com/arttonoyan/dotnet-sdk into feature/di-abstractions-builder-api-587
askpt
left a comment
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.
Great work! I only added a couple of minor comments.
Please have a chat with @kylejuliandev to try understand the impact on his deprecations efforts. I remember he mentioned a couple of fixes he was working on for the DI and I don't want that effort to get lost during this PR.
src/OpenFeature.Providers.DependencyInjection/OpenFeature.Providers.DependencyInjection.csproj
Show resolved
Hide resolved
src/OpenFeature.Providers.DependencyInjection/OpenFeatureProviderBuilderExtensions.cs
Show resolved
Hide resolved
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| private async Task InitializeProvidersAsync(CancellationToken cancellationToken) |
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.
cancellationToken seems to not be used here.
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.
I had always assumed SetProviderAsync would have a cancellationToken overload, but it doesn't appear like it does
dotnet-sdk/src/OpenFeature/Api.cs
Lines 37 to 66 in 7805184
| /// <summary> | |
| /// Sets the default feature provider. In order to wait for the provider to be set, and initialization to complete, | |
| /// await the returned task. | |
| /// </summary> | |
| /// <remarks>The provider cannot be set to null. Attempting to set the provider to null has no effect. May throw an exception if <paramref name="featureProvider"/> cannot be initialized.</remarks> | |
| /// <param name="featureProvider">Implementation of <see cref="FeatureProvider"/></param> | |
| public async Task SetProviderAsync(FeatureProvider featureProvider) | |
| { | |
| this._eventExecutor.RegisterDefaultFeatureProvider(featureProvider); | |
| await this._repository.SetProviderAsync(featureProvider, this.GetContext(), this.AfterInitialization, this.AfterError).ConfigureAwait(false); | |
| } | |
| /// <summary> | |
| /// Binds the feature provider to the given domain. In order to wait for the provider to be set, and | |
| /// initialization to complete, await the returned task. | |
| /// </summary> | |
| /// <remarks>The provider cannot be set to null. Attempting to set the provider to null has no effect. May throw an exception if <paramref name="featureProvider"/> cannot be initialized.</remarks> | |
| /// <param name="domain">An identifier which logically binds clients with providers</param> | |
| /// <param name="featureProvider">Implementation of <see cref="FeatureProvider"/></param> | |
| /// <exception cref="ArgumentNullException">domain cannot be null or empty</exception> | |
| public async Task SetProviderAsync(string domain, FeatureProvider featureProvider) | |
| { | |
| if (string.IsNullOrWhiteSpace(domain)) | |
| { | |
| throw new ArgumentNullException(nameof(domain)); | |
| } | |
| this._eventExecutor.RegisterClientFeatureProvider(domain, featureProvider); | |
| await this._repository.SetProviderAsync(domain, featureProvider, this.GetContext(), this.AfterInitialization, this.AfterError).ConfigureAwait(false); | |
| } |
Co-authored-by: André Silva <[email protected]> Signed-off-by: Artyom Tonoyan <[email protected]>
Co-authored-by: André Silva <[email protected]> Signed-off-by: Artyom Tonoyan <[email protected]>
Refactored `OpenFeatureOptions` to `OpenFeatureProviderOptions` to improve clarity and maintainability. Updated all references and test cases to use the new class. Removed inheritance of `OpenFeatureOptions` from `OpenFeatureProviderOptions` and introduced `_hookNames` for managing hook names. Replaced `TestOptions` with `TestProviderOptions` in `OpenFeatureBuilderExtensionsTests`, adding a `SomeFlag` property for more flexible testing. Renamed and updated `OpenFeatureOptionsTests` to `OpenFeatureProviderOptionsTests`. Performed general cleanup, including removing unused namespaces, ensuring consistent naming conventions, and aligning method calls with the new class structure.
src/OpenFeature.Providers.DependencyInjection/OpenFeature.Providers.DependencyInjection.csproj
Outdated
Show resolved
Hide resolved
Migrated `OpenFeature.DependencyInjection.Abstractions` to a new project, `OpenFeature.Providers.DependencyInjection`, to improve modularity and maintainability. Updated namespaces, project references, and `using` directives accordingly. Removed the deprecated `OpenFeature.DependencyInjection.Abstractions` project and its associated files. Introduced `OpenFeature.Providers.DependencyInjection.csproj` to house the migrated functionality. Updated test files to reference the new namespace and adjusted experimental feature annotations. Reorganized build configuration files to align with the new project structure. Ensured documentation and metadata reflect the refactored codebase.
|
@askpt @kylejuliandev I’ve made all the requested changes. I only left out the README.md. If that’s OK, I’ll handle it in a separate ticket. Please review again. Quick question: Should I apply the same changes to the DI package, or skip it because it’s obsolete? |
@arttonoyan just execute |
Removed `OpenFeature.Hosting.Internal` namespace from `FeatureLifecycleManagerTests.cs` and `OpenFeatureBuilderExtensionsTests.cs` as it is no longer required. Adjusted the placement of `OpenFeature.Providers.DependencyInjection` for consistency and improved readability. These changes streamline imports and enhance maintainability.
|
@askpt @kylejuliandev Could you please do a final review? I’d like to merge this PR. |
|
@arttonoyan Please update this action: https://github.com/open-feature/dotnet-sdk/blob/main/.github/workflows/release.yml. This package needs to be added to generate the security artefacts. |
Signed-off-by: Artyom Tonoyan <[email protected]>
done: 579ac49 |
Important
Update. 2025-11-10
Following the discussion in #596 (comment). the package name in this proposal is updated from
OpenFeature.DependencyInjectiontoOpenFeature.Providers.DependencyInjection. Rationale. clearer scope for provider authors. thinner provider packages. reduced coupling with the SDK.This PR
Extract the minimal, provider-agnostic DI surface into a new package:
OpenFeature.DependencyInjection.Abstractions.This isolates the contracts and lightweight wiring needed to integrate any OpenFeature provider without pulling in a concrete implementation.
Related Issues
Fixes #587
Notes
OpenFeatureOptionsinto a new base options type:OpenFeatureProviderOptions.OpenFeatureOptionsnow inherits fromOpenFeatureProviderOptions.OpenFeatureOptionsnow targetsOpenFeatureProviderOptions.Before (internal)
After (internal)
Impact
OpenFeatureOptions) require updates to targetOpenFeatureProviderOptions.How to test
This is a regression-only refactor.
Expectations