-
Notifications
You must be signed in to change notification settings - Fork 2
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.
// 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
}
}[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" });
}// 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.
The database feature management library provides support in ASP.NET Core and MVC to enable common feature flag scenarios in web applications.
// 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 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>services.AddControllersWithViews(options =>
{
options.Filters.AddForFeature<BetaFeatureFilter>("BetaFeatures");
});See more here.
- Use descriptive feature flag names (e.g.,
NewCheckoutProcessinstead ofFeature123) - 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