Skip to content

Consumption

Matteo Ciapparelli edited this page Apr 2, 2025 · 1 revision

The basic form of feature management is checking if a feature flag is enabled and then performing actions based on the result. This is done through the IFeatureManager's IsEnabledAsync method.

Basic Usage

// Inject the feature manager into your class
private readonly IFeatureManager _featureManager;

public MyService(IFeatureManager featureManager)
{
    _featureManager = featureManager;
}

public async Task DoSomethingAsync()
{
    if (await _featureManager.IsEnabledAsync("MyFeature"))
    {
        // Feature-specific implementation
    }
    else
    {
        // Default implementation
    }
}

Advanced Usage Patterns

Feature-gated API Endpoints

[HttpGet("beta-endpoint")]
public async Task<IActionResult> BetaEndpoint()
{
    if (!await _featureManager.IsEnabledAsync("BetaApi"))
    {
        return NotFound();
    }
    
    // Beta functionality implementation
    return Ok(new { status = "Beta feature active" });
}

Contextual Evaluation

// Using a feature with contextual filtering
public async Task<IActionResult> UserSpecificFeature([FromQuery] string userId)
{
    // Pass context data for targeting specific users
    var context = new FeatureFilterEvaluationContext
    {
        Parameters = new { UserId = userId }
    };
    
    if (await _featureManager.IsEnabledAsync("TargetedFeature", context))
    {
        return Ok("Feature enabled for this user");
    }
    
    return Ok("Feature not available for this user");
}

See more here.

ASP.NET Core Integration

The database feature management library provides support in ASP.NET Core and MVC to enable common feature flag scenarios in web applications.

Feature Attribute for Controllers and Actions

// Feature gate an entire controller
[Feature("BetaFeatures")]
public class BetaController : Controller
{
    // Controller actions
}

// Feature gate a specific action
public class ProductController : Controller
{
    [Feature("NewProductPage")]
    public IActionResult NewProductDetail(int id)
    {
        // New product detail implementation
        return View();
    }
    
    public IActionResult ProductDetail(int id)
    {
        // Standard product detail implementation
        return View();
    }
}

Feature Tag Helper for Views

<feature name="BetaUI">
    <div class="beta-ui">
        <!-- Beta UI elements here -->
    </div>
</feature>

<feature name="NewHeader" requirement="All">
    <div class="new-header">
        <!-- New header that requires all conditions to be met -->
    </div>
</feature>

Feature Policy for MVC Controllers

services.AddControllersWithViews(options => 
{
    options.Filters.AddForFeature<BetaFeatureFilter>("BetaFeatures");
});

See more here.

Best Practices

  • Use descriptive feature flag names (e.g., NewCheckoutProcess instead of Feature123)
  • Keep feature flags temporary whenever possible and remove them after full rollout
  • Test both enabled and disabled states of features
  • Use a centralized location for feature flag name constants
  • Consider a strategy for feature flag lifecycle management

Clone this wiki locally