Skip to content
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

feat: Feature Provider Enhancement - #321 #324

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

arttonoyan
Copy link
Contributor

@arttonoyan arttonoyan commented Nov 28, 2024

This update focuses on enhancing the feature provider integration and testing framework, incorporating improvements to flexibility, usability, and testing capabilities.
This update addresses GitHub Issue #321 in the OpenFeature .NET SDK repository.


Key Enhancements

  1. Dependency Injection (DI) Enhancements:

    • Improved lifecycle management for better resource handling.
    • Streamlined registration for feature providers, reducing configuration complexity.
    • Introduced the AddProvider extension method to simplify and adapt feature provider integration during service setup.
  2. Simplified Codebase:

    • Removed FeatureProviderFactory logic, eliminating unnecessary complexity and improving usability.
  3. Improved InMemoryProvider:

    • Enhanced the registration process for the InMemoryProvider, enabling smoother and more intuitive usage.
  4. Testing Improvements:

    • Established a dedicated integration test project for comprehensive validation.
    • Improved overall test coverage, ensuring the reliability and robustness of the framework.

Example: Service Registration and Usage

Service Registration

builder.Services.TryAddSingleton<IFeatureFlagConfigurationService, FlagConfigurationService>();

builder.Services.AddHttpContextAccessor();
builder.Services.AddOpenFeature(cfg =>
{
    cfg.AddHostedFeatureLifecycle();
    cfg.AddContext((builder, provider) =>
    {
        // Retrieve the HttpContext from IHttpContextAccessor, ensuring it's not null.
        var context = provider.GetRequiredService<IHttpContextAccessor>().HttpContext
            ?? throw new InvalidOperationException("HttpContext is not available.");

        var userId = UserInfoHelper.GetUserId(context);
        builder.Set("user", userId);
    });
    cfg.AddInMemoryProvider(provider =>
    {
        var flagService = provider.GetRequiredService<IFeatureFlagConfigurationService>();
        return flagService.GetFlags();
    });
});

FlagConfigurationService Implementation

public class FlagConfigurationService : IFeatureFlagConfigurationService
{
    private readonly IDictionary<string, Flag> _flags;

    public FlagConfigurationService()
    {
        _flags = new Dictionary<string, Flag>
        {
            {
                "feature-a", new Flag<bool>(
                    variants: new Dictionary<string, bool>
                    {
                        { "on", true },
                        { "off", false }
                    },
                    defaultVariant: "on", context => {
                        var id = context.GetValue("user").AsString;
                        if (id == null)
                        {
                            return "on"; // Default variant
                        }

                        return id == TestUserId ? "off" : "on";
                    })
            }
        };
    }

    public Dictionary<string, Flag> GetFlags() => _flags.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}

@arttonoyan arttonoyan requested a review from a team as a code owner November 28, 2024 20:23
@arttonoyan arttonoyan changed the title #321: improvement: Feature Provider Integration and Testing Framework #321: improvement: Feature Provider Integration Nov 28, 2024
Copy link

codecov bot commented Nov 29, 2024

Codecov Report

Attention: Patch coverage is 22.72727% with 51 lines in your changes missing coverage. Please review.

Project coverage is 86.66%. Comparing base (bf9de4e) to head (acd5c50).

Files with missing lines Patch % Lines
...ction/Providers/Memory/FeatureBuilderExtensions.cs 0.00% 42 Missing ⚠️
...ependencyInjection/OpenFeatureBuilderExtensions.cs 61.90% 7 Missing and 1 partial ⚠️
...ection/Providers/Memory/InMemoryProviderOptions.cs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #324      +/-   ##
==========================================
+ Coverage   85.91%   86.66%   +0.75%     
==========================================
  Files          34       34              
  Lines        1377     1387      +10     
  Branches      148      147       -1     
==========================================
+ Hits         1183     1202      +19     
+ Misses        162      153       -9     
  Partials       32       32              

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

@arttonoyan arttonoyan changed the title #321: improvement: Feature Provider Integration #321: feat: Feature Provider Integration Nov 29, 2024
@arttonoyan arttonoyan changed the title #321: feat: Feature Provider Integration #321: feat: Feature Provider Enhancement Nov 29, 2024
@arttonoyan arttonoyan changed the title #321: feat: Feature Provider Enhancement feat: Feature Provider Enhancement - #321 Nov 29, 2024
@arttonoyan
Copy link
Contributor Author

@toddbaert @askpt @beeme1mr Hi guys, I hope you are doing well! 🖐
I recently submitted this PR that addresses the #321 issue. It's been a few days, and I wanted to kindly request that someone take a look when they have a moment.
Your feedback is greatly appreciated.
Thanks!

Copy link
Member

@askpt askpt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! It seems that some of the DI extension classes are missing coverage. I wonder if it is worth excluding them from code coverage for the build to pass.

Copy link
Member

@beeme1mr beeme1mr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @arttonoyan, sorry for the delay. Last week was a holiday in the US.

The change looks good to me. Should the examples in the readme also be updated?

@arttonoyan
Copy link
Contributor Author

Hey @arttonoyan, sorry for the delay. Last week was a holiday in the US.

The change looks good to me. Should the examples in the readme also be updated?

Oh, no problem! I forgot about that. 😊 Yes, of course, I’ll update the documentation as well. I just wanted to get your feedback first, and I’ll make sure to update it before merging.

@toddbaert
Copy link
Member

@ghelyar does this solve the issues you mentioned?

@ghelyar
Copy link

ghelyar commented Dec 3, 2024

yes

Copy link
Member

@toddbaert toddbaert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me and addresses the outside feedback.

My only blocker is updated documentation. Once you have that I can squash down the commits to one for you to sign off on if you want, @arttonoyan .

@arttonoyan
Copy link
Contributor Author

This looks good to me and addresses the outside feedback.

My only blocker is updated documentation. Once you have that I can squash down the commits to one for you to sign off on if you want, @arttonoyan .

Thanks, @toddbaert ! I’ve updated the document, and I think this version is much more flexible - I’m really happy with how it’s shaping up. It’s great to see external feedback coming in; it’s been super helpful.
For this release, I also added integration tests, which made spotting issues so much easier. I’m sure we’ll keep improving it even more in the future!
cc: @beeme1mr @askpt @ghelyar

arttonoyan and others added 7 commits December 4, 2024 08:35
Signed-off-by: Artyom Tonoyan <[email protected]>
Signed-off-by: Artyom Tonoyan <[email protected]>
…e#323)

When provider resolutions with error code set other than `None` are returned, the provider acts as if an error was thrown.

Signed-off-by: christian.lutnik <[email protected]>
Signed-off-by: Artyom Tonoyan <[email protected]>
@arttonoyan arttonoyan force-pushed the feature/321-dependency-injection-service-provider branch from acd5c50 to e383697 Compare December 4, 2024 04:36
@beeme1mr
Copy link
Member

beeme1mr commented Dec 4, 2024

Hey @toddbaert, it looks like this PR is ready! Could you please take a look when you have a moment?

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.

[FEATURE] dependency injection: use of service provider with feature providers
6 participants