Home - TFeatureManagement/TFeatureManagement GitHub Wiki
TFeatureManagement extends the Microsoft.FeatureManagement and Microsoft.FeatureManagement.AspNetCore libraries to add support for using enums to define and reference feature flags. It does so by implementing generic classes, methods and interfaces (hence the name TFeatureManagement) that wrap the Microsoft.FeatureManagement and Microsoft.FeatureManagement.AspNetCore classes, methods and interfaces. These generic classes, methods and interfaces ensure the use of an enum to define and consume feature flags.
As TFeatureManagement extends the Microsoft.FeatureManagement and Microsoft.FeatureManagement.AspNetCore libraries, it is recommended that you familiarise yourself with those libraries in addition to reading this wiki as a lot of the concepts and functionality in those libraries apply to TFeatureManagement as well. The project website and source repository for those libraries can be found in the Microsoft.FeatureManagement GitHub repo.
NOTE: This wiki is a work in progress and will be split up into separate pages over time.
As TFeatureManagement extends the Microsoft.FeatureManagement and Microsoft.FeatureManagement.AspNetCore libraries, the .NET Core configuration system is used to determine the state of feature flags. See Feature Flags for more information on declaring feature flags using the .NET Core configuration system.
To make it possible to reference the configured feature flags in code, define feature flag variables like below.
// Define feature flags in an enum
public enum Feature
{
FeatureX,
FeatureY,
FeatureZ
}
Feature flags rely on .NET Core dependency injection. We can register the feature management services using standard conventions.
For a .NET Core Application:
using TFeatureManagement.DependencyInjection;
using Microsoft.FeatureManagement.FeatureFilters;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddFeatureManagement<Feature>()
.AddFeatureFilter<PercentageFilter>()
.AddFeatureFilter<TimeWindowFilter>();
}
}
For an ASP.NET Core Application:
using TFeatureManagement.AspNetCore.DependencyInjection;
using Microsoft.FeatureManagement.FeatureFilters;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddFeatureManagement<Feature>()
.AddFeatureFilter<PercentageFilter>()
.AddFeatureFilter<TimeWindowFilter>();
}
}
Note: TFeatureManagement supports all built-in Microsoft.FeatureManagement feature filters.
The simplest use case for feature flags is to do a conditional check for whether a feature is enabled to take different paths in code. The use cases grow from there as the feature flag API begins to offer extensions into ASP.NET Core.
The basic form of feature management is checking if a feature is enabled and then performing actions based on the result. This is done through the IFeatureManager<TFeature>
's IsEnabledAsync
method.
…
IFeatureManager<Feature> featureManager;
…
if (await featureManager.IsEnabledAsync(Feature.FeatureX))
{
// Do something
}
The IFeatureManager<TFeature>
's IsEnabledAsync
method can also be used to check if a set of features are enabled.
…
IFeatureManager<Feature> featureManager;
…
if (await featureManager.IsEnabledAsync(Feature.FeatureX, Feature.FeatureY))
{
// Do something
}
The above example checks whether all the features are enabled, but the method can also be used to checked whether any of features are enabled.
…
IFeatureManager<Feature> featureManager;
…
if (await featureManager.IsEnabledAsync(RequirementType.Any, Feature.FeatureX, Feature.FeatureY))
{
// Do something
}
When using the feature management library with MVC, the IFeatureManager<TFeature>
can be obtained through dependency injection.
public class HomeController : Controller
{
private readonly IFeatureManager<Feature> _featureManager;
public HomeController(IFeatureManager<Feature> featureManager)
{
_featureManager = featureManager;
}
}
See ASP.NET Core for more information on usages specific to ASP.NET Core.