Trunk-Based Development and Feature Flags in NET Core

Listen to this Post

Trunk-based development is a branching strategy where all developers work directly on the “trunk” (main branch). Feature flags are essential in this approach to hide incomplete features until they are ready for release. ASP.NET Core provides built-in support for feature flags, making it easier to manage feature toggles.

You Should Know:

To get started with feature flags in ASP.NET Core, follow these steps:

1. Install the Microsoft.FeatureManagement NuGet package:

dotnet add package Microsoft.FeatureManagement

2. Configure feature flags in `appsettings.json`:

{
"FeatureManagement": {
"FeatureA": true,
"FeatureB": false
}
}

3. Register the feature management services in `Startup.cs`:

public void ConfigureServices(IServiceCollection services)
{
services.AddFeatureManagement();
}
  1. Use feature flags in your controllers or views:
    [FeatureGate("FeatureA")]
    public IActionResult FeatureA()
    {
    return View();
    }
    

5. Conditional rendering in Razor views:

@using Microsoft.FeatureManagement
@inject IFeatureManager FeatureManager

@if (await FeatureManager.IsEnabledAsync("FeatureA"))
{
Feature A is enabled!
}

Practice Verified Commands and Steps:

  • Enable a feature flag dynamically:
    await FeatureManager.EnableAsync("FeatureA");
    

  • Disable a feature flag dynamically:

    await FeatureManager.DisableAsync("FeatureA");
    

  • Check feature flag status:

    bool isEnabled = await FeatureManager.IsEnabledAsync("FeatureA");
    

  • Use feature flags in middleware:

    app.Use(async (context, next) =>
    {
    if (await FeatureManager.IsEnabledAsync("FeatureA"))
    {
    await context.Response.WriteAsync("Feature A is enabled!");
    }
    await next();
    });
    

What Undercode Say:

Trunk-based development, combined with feature flags, offers a streamlined approach to continuous integration and delivery. By leveraging ASP.NET Core’s built-in support for feature management, teams can effectively manage feature rollouts and ensure a stable main branch. However, this approach requires discipline and regular cleanup of feature flags to avoid technical debt.

For further reading, check out the official documentation on Feature Management in ASP.NET Core.

Related Linux/Windows Commands: