Skip to content

Conversation

@askpt
Copy link
Member

@askpt askpt commented Jul 29, 2025

This PR

This pull request refactors the Api class and its integration with FeatureClient to improve testability and dependency management by replacing the singleton access pattern with dependency injection. It also includes minor project configuration updates. Below are the key changes grouped by theme:

Refactoring Api and Dependency Injection:

  • Changed the Api constructor from private to internal, allowing it to be instantiated directly for dependency injection purposes (src/OpenFeature/Api.cs, src/OpenFeature/Api.csL35-R35).
  • Updated AddOpenFeature in OpenFeatureServiceCollectionExtensions to register a new instance of Api instead of using the singleton Api.Instance (src/OpenFeature.DependencyInjection/OpenFeatureServiceCollectionExtensions.cs, src/OpenFeature.DependencyInjection/OpenFeatureServiceCollectionExtensions.csL27-R27).
  • Modified FeatureClient to accept an Api instance as a constructor parameter, defaulting to Api.Instance if not provided. Updated all internal references to use the injected Api instance (src/OpenFeature/OpenFeatureClient.cs, [1] [2] [3] [4] [5].

Updates to FeatureClient:

  • Added a private _api field to store the injected Api instance and replaced all direct calls to Api.Instance with _api to improve testability (src/OpenFeature/OpenFeatureClient.cs, [1] [2] [3].

Project Configuration:

  • Added InternalsVisibleTo for OpenFeature.DependencyInjection to allow access to internal members of the OpenFeature assembly (src/OpenFeature/OpenFeature.csproj, src/OpenFeature/OpenFeature.csprojR22).

Related Issues

Fixes #533

Notes

As pointed out, I made a couple of changes in the FeatureClient class to use the same reference all the time for the Api. I checked the rest of the codebase and the Api.Instance is only used in the testing projects.
This change doesn't affect any external methods.

askpt added 2 commits July 29, 2025 06:24
Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com>
…nd context retrieval

Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com>
@askpt askpt changed the title bugfix: Fix DI lifecycle to use container instead of static instance fix: Fix DI lifecycle to use container instead of static instance Jul 29, 2025
@codecov
Copy link

codecov bot commented Jul 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.48%. Comparing base (2547a57) to head (bc4ff7c).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #534      +/-   ##
==========================================
+ Coverage   88.45%   88.48%   +0.03%     
==========================================
  Files          50       50              
  Lines        2096     2102       +6     
  Branches      245      245              
==========================================
+ Hits         1854     1860       +6     
  Misses        191      191              
  Partials       51       51              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@askpt askpt requested a review from kinyoklion July 29, 2025 16:15
@askpt askpt marked this pull request as ready for review July 29, 2025 16:16
@askpt askpt requested a review from a team as a code owner July 29, 2025 16:16
@beeme1mr beeme1mr changed the title fix: Fix DI lifecycle to use container instead of static instance fix: update DI lifecycle to use container instead of static instance Jul 29, 2025
@askpt askpt requested a review from toddbaert July 29, 2025 20:16
askpt added 2 commits July 29, 2025 22:04
…jection

Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com>
…nd improve provider access

Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Non-Static-Singleton in OpenFeature.Hosting

4 participants