15. FeatureFlagGen
Strongly-typed feature flags generated from config --- no more magic strings.
Language C# / .NET 8+
Distribution NuGet (dotnet add package FeatureFlagGen)
Build Time 3--5 days
License MIT
Category Source Generator / C#
Problem
Microsoft.FeatureManagement uses string-based flag names: IsEnabledAsync("DarkMode"). These strings are error-prone, invisible to refactoring tools, and produce runtime errors on typos. Developers want the same compile-time safety that [LoggerMessage] brought to logging --- but for feature flags.
Solution
A Roslyn source generator that reads feature flag definitions from appsettings.json or a dedicated flags.json file at compile time and generates a strongly-typed IFeatureFlags interface with bool properties, extension methods for IFeatureManager, and DI registration code.
Core Features
-
Config-driven generation: Reads FeatureManagement section from appsettings.json or a dedicated flags.json at compile time.
-
Strongly-typed interface: Generates IFeatureFlags with IsXxxEnabled properties for each flag. Null-safe, refactorable, IntelliSense-friendly.
-
Extension methods: Generates IsXxxEnabledAsync() extension methods on IFeatureManager for gradual migration from string-based code.
-
DI registration: Generates AddFeatureFlags() extension method for IServiceCollection that wires up the implementation.
-
Flag groups: Supports flag grouping via JSON nesting. Generates nested classes: Flags.Ui.IsDarkModeEnabled.
-
Compile-time validation: Reports a diagnostic if a flag name in code doesn't match any flag in the config file.
Technical Architecture
Roslyn incremental source generator that uses AdditionalFiles to read JSON config at compile time. Parses the FeatureManagement section, builds a flag model, and generates C# source for the interface, implementation, and DI extensions. The implementation wraps IFeatureManager with the generated property names as string constants (keeping runtime compatibility). Incremental generation ensures rebuilds only happen when the JSON changes.
CLI / API Surface
// appsettings.json
{ "FeatureManagement": { "DarkMode": true, "NewCheckout": false } }
// Generated interface
public interface IFeatureFlags {
bool IsDarkModeEnabled { get; }
bool IsNewCheckoutEnabled { get; }
}
// Usage
if (flags.IsDarkModeEnabled) { ... }
Key Dependencies
-
Microsoft.CodeAnalysis.CSharp
-
System.Text.Json (for config parsing at compile time)
Scope Boundaries
In scope: appsettings.json and flags.json reading. Interface + implementation generation. DI registration. Flag groups.
Out of scope: Runtime flag evaluation logic (delegates to Microsoft.FeatureManagement). Feature flag management UI. A/B testing. Percentage rollouts.
Success Criteria
-
Generates correct code for a 50-flag appsettings.json
-
Compile-time error when referencing a non-existent flag
-
Works with Microsoft.FeatureManagement without changes
-
Published on NuGet with README showing migration from string-based flags