-
Notifications
You must be signed in to change notification settings - Fork 2
Getting Started
.NET Feature Management Database allows you to manage feature definitions stored in a database.
The built-in DatabaseFeatureDefinitionProvider retrieves these definitions and converts them into FeatureDefinition objects used by the feature management system.
This setup relies on an implementation of the IFeatureStore interface to access the database.
Two primary entities are pre-configured for database feature management:
-
Feature: represents a feature with its associated settings. Each feature has a unique name, a requirement type, and a collection of settings that define how the feature is enabled or disabled.
-
FeatureSettings: contains the settings for a feature and these define the conditions under which a feature is enabled. The condition parameters are stored in JSON format and based on Feature Management built-in feature filter or contextual feature filter configuration, and can include custom feature filter configuration.
Suppose you want to define a feature that is enabled for 50% of the users. Here is an example of how you can define such a feature and its settings:
// Create a new feature with percentage-based targeting
var newFeature = new Feature
{
Id = Guid.NewGuid(),
Name = "NewFeature",
RequirementType = RequirementType.Any, // Any requirement can enable the feature
Settings = new List<FeatureSettings>
{
new FeatureSettings
{
Id = Guid.NewGuid(),
FilterType = FeatureFilterType.Percentage,
Parameters = "{\"Value\":50}" // 50% of users will see this feature
}
}
}
// Add to your data store
await featureStore.AddFeatureAsync(newFeature);The IFeatureStore interface is the core abstraction for retrieving feature data from a database.
Implement this interface to connect to your specific database (e.g., SQL Server, MongoDB, etc.).
public class MyFeatureStore : IFeatureStore
{
private readonly MyDbContext _dbContext;
public MyFeatureStore(MyDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<IReadOnlyCollection<Feature>> GetFeaturesAsync()
{
return await _dbContext.Features
.Include(f => f.Settings)
.ToListAsync();
}
public async Task<Feature> GetFeatureAsync(string featureName)
{
return await _dbContext.Features
.Include(f => f.Settings)
.FirstOrDefaultAsync(f => f.Name == featureName);
}
}See built-in database implementations for ready-to-use implementations.
Database feature management relies on .NET Core dependency injection. You can register the necessary services with a single line of code:
services.AddDatabaseFeatureManagement<MyFeatureStore>();Note
In the context of database solutions, the feature management services will be added as scoped services.
Important
To use database feature management, you need to register an implementation of IFeatureStore.
Enhance performance and reduce database load using WithCacheService:
services.AddDatabaseFeatureManagement<MyFeatureStore>()
.WithCacheService();By default, the inactive cache will be removed after 30 minutes.
services.AddDatabaseFeatureManagement<MyFeatureStore>()
.WithCacheService(options =>
{
options.SlidingExpiration = TimeSpan.FromMinutes(10);
options.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1);
});var cacheConfiguration = Configuration.GetSection(FeatureCacheOptions.Name);
services.AddDatabaseFeatureManagement<MyFeatureStore>()
.WithCacheService(cacheConfiguration);And in your appsettings.json:
{
"FeatureCacheOptions": {
"AbsoluteExpirationRelativeToNow": "01:00:00",
"SlidingExpiration": "00:30:00"
}
}The cache keys have a prefix (FMDb_) defined in the options (FeatureCacheOptions.CachePrefix). By default:
| Single feature | All features | |
|---|---|---|
| Key | FMDb_MyFeature | FMDb_features |
Note that "features" can be overridden when configuring cache. So you can have "FMDb_<your-custom-cache-key>".
See the FeatureCacheOptions class for more cache-related settings.
Warning
Cache does not auto-refresh when feature values update directly in the database. Handle cache invalidation appropriately.
Important
When using .NET 9, cache entries will automatically leverage the unified caching approach of HybridCache. This provides multi-level caching with in-memory and distributed cache support out of the box. The expiration settings from FeatureCacheOptions (like SlidingExpiration and AbsoluteExpirationRelativeToNow) are automatically applied to the cache entries. This optimization happens transparently in the CachedFeatureStore implementation, requiring no additional configuration from you.
- Learn how to consume feature flags in your application
- Explore the different database providers
- Check the available packages