muench-develops.FeatureToggler 1.0.3

dotnet add package muench-develops.FeatureToggler --version 1.0.3                
NuGet\Install-Package muench-develops.FeatureToggler -Version 1.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="muench-develops.FeatureToggler" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add muench-develops.FeatureToggler --version 1.0.3                
#r "nuget: muench-develops.FeatureToggler, 1.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install muench-develops.FeatureToggler as a Cake Addin
#addin nuget:?package=muench-develops.FeatureToggler&version=1.0.3

// Install muench-develops.FeatureToggler as a Cake Tool
#tool nuget:?package=muench-develops.FeatureToggler&version=1.0.3                

muench-develops.FeatureToggler

muench-develops.FeatureToggler is a lightweight and extensible library for managing feature flags in .NET applications. It allows you to dynamically enable or disable features using multiple providers.

Features

  • Multiple Providers: In-Memory, JSON, Environment Variables.
  • ASP.NET Core Integration: Middleware for feature-based request handling.
  • Extensible: Easily add custom providers.
  • Thread-Safe: Safe for concurrent updates and queries.

Installation

Install via NuGet:

dotnet add package muench-develops.FeatureToggler

Quick Start

1. Register Feature Flags

In-Memory Provider

For quick development and testing:

builder.Services.AddFeatureFlags(
    new InMemoryFeatureFlagProvider(
        new[]
        {
            new FeatureFlag("EnableNewUI", true, "Enables the new UI"),
            new FeatureFlag("BetaFeature", false, "Beta feature toggle")
        })
);
JSON Provider

To manage feature flags via a JSON file, use JsonFeatureFlagProvider:

  1. Create a JSON file, e.g., featureflags.json:
[
    { "Key": "EnableNewUI", "IsEnabled": true, "Description": "Enables the new UI" },
    { "Key": "BetaFeature", "IsEnabled": false, "Description": "Beta feature toggle" }
]
  1. Register the provider in Program.cs:
builder.Services.AddFeatureFlags(
    new JsonFeatureFlagProvider("path/to/featureflags.json", new FileReader())
);
Environment Variable Provider

Use environment variables to control feature flags dynamically:

  1. Set environment variables:
$env:ENABLE_NEW_UI = "true"
$env:BETA_FEATURE = "false"
  1. Register the provider in Program.cs:
builder.Services.AddFeatureFlags(
    new EnvironmentVariableFeatureFlagProvider(new Dictionary<string, string>
    {
        { "EnableNewUI", "ENABLE_NEW_UI" },
        { "BetaFeature", "BETA_FEATURE" }
    })
);
Configuration Provider

The ConfigurationFeatureFlagProvider enables you to manage feature toggles directly from the .NET IConfiguration system, which can include sources like appsettings.json, environment variables, or other configuration providers.

{
  "FeatureToggles": {
    "EnableNewUI": {
      "IsEnabled": true,
      "Description": "Enables the new UI"
    },
    "BetaFeature": {
      "IsEnabled": false,
      "Description": "This is a beta feature toggle"
    }
  }
}

using FeatureToggler.Providers;

var builder = WebApplication.CreateBuilder(args);

// Register Feature Flags with the Configuration Provider
builder.Services.AddFeatureFlags(
    new ConfigurationFeatureFlagProvider(builder.Configuration)
);

var app = builder.Build();
2a. Use Middleware

Execute actions based on feature flag states:

app.UseFeatureFlag("EnableNewUI",
    onEnabled: () => Console.WriteLine("New UI is enabled!"),
    onDisabled: () => Console.WriteLine("New UI is disabled!"));
2b. Use Minimal API
app.MapGet("/", (FeatureFlagManager manaer) => manager.IsEnabled("EnableNewUI")
        ? Results.Ok("Welcome to the new UI!")
        : Results.Ok("Welcome to the old UI!"));
3. Query Flags

Check or update feature flags dynamically:

var manager = app.Services.GetRequiredService<FeatureFlagManager>();

if (manager.IsEnabled("EnableNewUI"))
{
    Console.WriteLine("New UI is enabled!");
}

License

This project is licensed under the Apache License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 99 12/21/2024
1.0.1 101 12/21/2024
1.0.0 95 12/21/2024