Skip to content

Implement support for ASP.NET core Feature Management #55

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 4 commits into from
Feb 21, 2025

Conversation

haacked
Copy link
Collaborator

@haacked haacked commented Feb 21, 2025

This includes support for variants as well.

Fixes #37

Feature Management

PostHog.AspNetCore supports .NET Feature Management. This allows you to use the <feature /> tag helper and
the FeatureGateAttribute in your ASP.NET Core applications to gate access to certain features using PostHog
feature flags.

Setup

To use feature flags with the .NET Feature Management library, you'll need to implement the
IPostHogFeatureFlagContextProvider interface. The quickest way to do that is to inherit from the
PostHogFeatureFlagContextProvider class and override the GetDistinctId and GetFeatureFlagOptionsAsync methods.

public class MyFeatureFlagContextProvider(IHttpContextAccessor httpContextAccessor)
    : PostHogFeatureFlagContextProvider
{
    protected override string? GetDistinctId() => httpContextAccessor.HttpContext?.User.Identity?.Name;
    
    protected override ValueTask<FeatureFlagOptions> GetFeatureFlagOptionsAsync()
    {
        // In a real app, you might get this information from a database or other source for the current user.
        return ValueTask.FromResult(
            new FeatureFlagOptions
            {
                PersonProperties = new Dictionary<string, object?>
                {
                    ["email"] = "[email protected]"
                },
                OnlyEvaluateLocally = true
            });
    }
}

Then, register your implementation in Program.cs (or Startup.cs):

var builder = WebApplication.CreateBuilder(args);
builder.AddPostHog(options => {
    options.UseFeatureManagement<MyFeatureFlagContextProvider>();
});

Usage

You can now use feature tag helpers in your Razor views:

<feature name="awesome-new-feature">
    <p>This is the new feature!</p>
</feature>
<feature name="awesome-new-feature" negate="true">
    <p>Sorry, no awesome new feature for you.</p>
</feature>

Multivariate feature flags are also supported:

<feature name="awesome-new-feature" value="variant-a">
    <p>This is the new feature variant A!</p>
</feature>
<feature name="awesome-new-feature" value="variant-b">
    <p>This is the new feature variant B!</p>
</feature>

You can also use the FeatureGateAttribute to gate access to controllers or actions:

[FeatureGate("awesome-new-feature")]
public class NewFeatureController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

This PR implements ASP.NET Core Feature Management support in the PostHog .NET SDK, enabling feature flag management through tag helpers and attributes in ASP.NET Core applications.

  • Added PostHogFeatureFlagContextProvider base class in /src/PostHog.AspNetCore/FeatureManagement/ for evaluating feature flags with customizable user identification and options
  • Implemented PostHogVariantFeatureManager in /src/PostHog.AspNetCore/FeatureManagement/PostHogVariantFeatureManager.cs to handle multivariate feature flags and variants
  • Created PostHogFeatureFilter and PostHogFeatureDefinitionProvider in /src/PostHog.AspNetCore/FeatureManagement/ to integrate with ASP.NET Core's feature management system
  • Added comprehensive unit tests in /tests/UnitTests.AspNetCore/FeatureManagement/ covering feature flag evaluation, variants, and targeting
  • Updated /src/PostHog.AspNetCore/README.md with detailed documentation on setup and usage of feature management capabilities

30 file(s) reviewed, 19 comment(s)
Edit PR Review Bot Settings | Greptile

@haacked haacked merged commit 34777fe into main Feb 21, 2025
4 checks passed
@haacked haacked deleted the haacked/37-feature-management branch February 21, 2025 22:51
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.

Implement Asp.NET Core Feature Management APIs
1 participant