Skip to content

ndcorder/FeatureFlagGen

Repository files navigation

FeatureFlagGen

Strongly-typed feature flags generated from config — no more magic strings.

A Roslyn source generator that reads feature flag definitions from appsettings.json or flags.json at compile time and generates:

  • A strongly-typed IFeatureFlags interface with bool properties
  • Extension methods for IFeatureManager
  • DI registration via AddFeatureFlags()
  • String constants for flag names

Why?

Microsoft.FeatureManagement uses magic strings:

// Before: runtime errors on typos, invisible to refactoring
if (await featureManager.IsEnabledAsync("DarkMode")) { ... }

FeatureFlagGen gives you compile-time safety:

// After: IntelliSense, refactorable, compile-time checked
if (flags.IsDarkModeEnabled) { ... }

Installation

dotnet add package FeatureFlagGen

Quick Start

1. Define flags in appsettings.json

{
  "FeatureManagement": {
    "DarkMode": true,
    "NewCheckout": false
  }
}

2. Reference the config as an AdditionalFile

In your .csproj:

<ItemGroup>
  <AdditionalFiles Include="appsettings.json" />
</ItemGroup>

3. Register in DI

builder.Services.AddFeatureManagement();
builder.Services.AddFeatureFlags(); // generated extension method

4. Inject and use

public class MyService
{
    private readonly IFeatureFlags _flags;

    public MyService(IFeatureFlags flags)
    {
        _flags = flags;
    }

    public void DoWork()
    {
        if (_flags.IsDarkModeEnabled)
        {
            // dark mode logic
        }
    }
}

Flag Groups

Nest flags in JSON to create grouped access:

{
  "FeatureManagement": {
    "Ui": {
      "DarkMode": true,
      "SideBar": false
    }
  }
}

Access via nested properties:

if (flags.Ui.IsDarkModeEnabled) { ... }

Generated Code

FeatureFlagGen produces five source files:

File Contents
IFeatureFlags.g.cs Interface with IsXxxEnabled properties
FeatureFlags.g.cs Implementation wrapping IFeatureManager
FeatureFlagExtensions.g.cs IsXxxEnabledAsync() extension methods on IFeatureManager
FeatureFlagServiceCollectionExtensions.g.cs AddFeatureFlags() DI registration
FeatureFlagNames.g.cs String constants for flag names

Extension Methods

For gradual migration, use the generated extension methods directly on IFeatureManager:

if (await featureManager.IsDarkModeEnabledAsync()) { ... }

String Constants

Access flag name strings via generated constants:

var name = FeatureFlagNames.DarkMode; // "DarkMode"
var grouped = FeatureFlagNames.Ui.DarkMode; // "Ui.DarkMode"

Filter-Based Flags

Filter-style flag definitions are fully supported:

{
  "FeatureManagement": {
    "Beta": {
      "EnabledFor": [
        { "Name": "Percentage", "Parameters": { "Value": 50 } }
      ]
    }
  }
}

These are treated as flat flags and generate IsBetaEnabled properties.

Supported Config Files

The generator recognizes these file names as AdditionalFiles:

  • appsettings.json
  • appsettings.*.json (e.g., appsettings.Development.json)
  • flags.json

Diagnostics

ID Severity Description
FFG001 Warning No FeatureManagement section found in config file
FFG002 Error Invalid JSON in configuration file
FFG003 Error Duplicate feature flag name

Requirements

  • .NET 8.0 or later
  • Microsoft.FeatureManagement package in your application

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feat/my-feature)
  3. Write tests for your changes
  4. Ensure all tests pass: dotnet test
  5. Ensure formatting: dotnet format --verify-no-changes
  6. Submit a pull request

License

MIT — see LICENSE for details.

About

Source generator for strongly-typed feature flags from appsettings.json — no more magic strings

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages