Skip to content

feat: Add Extension Method for adding global Hook via DependencyInjection #459

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

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

Conversation

kylejuliandev
Copy link
Contributor

This PR

  • This adds a new OpenFeatureBuilder extension method to add global or not domain-bound hooks to the OpenFeature Api.

Related Issues

Fixes #456

Notes

We inject any provided Hooks as Singletons in the DI container. We use keyed singletons and use the class name as the key. Maybe we'd want to use a different name to avoid conflicts?

I've done some manual testing with a sample weatherforecast ASP.NET Core web application

Follow-up Tasks

How to test

Signed-off-by: Kyle Julian <38759683+kylejuliandev@users.noreply.github.com>
Signed-off-by: Kyle Julian <38759683+kylejuliandev@users.noreply.github.com>
@kylejuliandev kylejuliandev requested a review from a team as a code owner April 29, 2025 19:30
/// </summary>
/// <typeparam name="THook">The type of<see cref="Hook"/> to be added.</typeparam>
/// <param name="builder">The <see cref="OpenFeatureBuilder"/> instance.</param>
/// <param name="implementationFactory"></param>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Missing description for implementationFactory

public static OpenFeatureBuilder AddHook<THook>(this OpenFeatureBuilder builder, Func<IServiceProvider, THook> implementationFactory)
where THook : Hook
{
var hookName = typeof(THook).Name;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FeatureProvider's have a name or metadata tag which we use as the key when injecting them into the DI container. Here we use the Hook class name. Maybe we want to use something else incase hooks clash?

Copy link
Member

Choose a reason for hiding this comment

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

They are being added as singleton. So if the hook already exists in the container should we really add them again? I would vote to do a continue and ignore adding more instances.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

More that if two hooks have the same class name, then only one will actually be injected into the DI container

Not sure if there are many hooks out there, and whether consumers would use both hooks. Not sure if we should add a caution or warning about it in the xml comment

Copy link

codecov bot commented Apr 29, 2025

Codecov Report

Attention: Patch coverage is 93.33333% with 2 lines in your changes missing coverage. Please review.

Project coverage is 86.59%. Comparing base (6a8b00a) to head (cbd7aa4).

Files with missing lines Patch % Lines
...ependencyInjection/OpenFeatureBuilderExtensions.cs 86.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #459      +/-   ##
==========================================
+ Coverage   86.47%   86.59%   +0.12%     
==========================================
  Files          42       42              
  Lines        1671     1701      +30     
  Branches      177      179       +2     
==========================================
+ Hits         1445     1473      +28     
- Misses        187      189       +2     
  Partials       39       39              

☔ 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.

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.

Looks good to me. Please look into the suggestions!

/// <param name="builder">The <see cref="OpenFeatureBuilder"/> instance.</param>
/// <param name="implementationFactory"></param>
/// <returns></returns>
public static OpenFeatureBuilder AddHook<THook>(this OpenFeatureBuilder builder, Func<IServiceProvider, THook> implementationFactory)
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should make implementationFactory optional. Then we can add a new hook using Services.AddHook<NoOpHook>(). In cases where the dependencies are already declared in the container, we should make use of it and not create a new ones.

public static OpenFeatureBuilder AddHook<THook>(this OpenFeatureBuilder builder, Func<IServiceProvider, THook> implementationFactory)
where THook : Hook
{
var hookName = typeof(THook).Name;
Copy link
Member

Choose a reason for hiding this comment

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

They are being added as singleton. So if the hook already exists in the container should we really add them again? I would vote to do a continue and ignore adding more instances.

* This will allow consumers to easily add Hooks without forcing them to
  write a function everytime they do

Signed-off-by: Kyle Julian <38759683+kylejuliandev@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.

[FEATURE] Add Hooks to the global instance of OpenFeature using DI syntax
2 participants