|
1 |
| -# FeatureManagement.Database |
2 |
| -An extension library of .NET Feature Management for implementing feature flags in a .NET or ASP.NET Core application through database. |
| 1 | +# <img height="55" src="\build\icon.png" align="center"> .NET Database Feature Management |
| 2 | + |
| 3 | + |
| 4 | +[](https://github.com/teociaps/FeatureManagement.Database/actions/workflows/build.yml) |
| 5 | +[](https://github.com/teociaps/FeatureManagement.Database/actions/workflows/test.yml) |
| 6 | + |
| 7 | +**.NET Feature Management Database** extends [Feature Management] for retrieving feature definitions from various databases. |
| 8 | +It includes abstractions and default implementations to facilitate easy integration with your .NET applications. |
| 9 | + |
| 10 | + |
| 11 | +## Supported .NET Versions |
| 12 | + |
| 13 | +| Version | Status | |
| 14 | +| ------- | ------ | |
| 15 | +| .NET 6 |  | |
| 16 | +| .NET 7 |  | |
| 17 | +| .NET 8 |  | |
| 18 | + |
| 19 | + |
| 20 | +## Index |
| 21 | + |
| 22 | +* [Features](#features) |
| 23 | +* [Packages](#packages) |
| 24 | +* [Getting Started](#getting-started) |
| 25 | + * [Feature Store](#feature-store) |
| 26 | + * [Service Registration](#service-registration) |
| 27 | + * [Configure Cache](#configure-cache) |
| 28 | +* [Consumption](#consumption) |
| 29 | + * [ASP.NET Core Integration](#asp.net-core-integration) |
| 30 | + |
| 31 | + |
| 32 | +## Features |
| 33 | + |
| 34 | +- **Database Integration**: store and retrieve feature definitions from various databases. |
| 35 | +- **Caching**: built-in support for caching feature definitions to enhance performance and reduce database load. |
| 36 | +- **Customizable**: easily extend and customize to support additional storage solutions and unique requirements. |
| 37 | +- **Seamless Integration**: integrates smoothly with Microsoft Feature Management, enabling efficient database-backed feature flag management. |
| 38 | + |
| 39 | + |
| 40 | +## Packages |
| 41 | + |
| 42 | +| Package | NuGet Version | |
| 43 | +| ------- | ------------- | |
| 44 | +| [FeatureManagement.Database.Abstractions](https://www.nuget.org/packages/FeatureManagement.Database.Abstractions/) | [](https://www.nuget.org/packages/FeatureManagement.Database.Abstractions/) |
| 45 | + |
| 46 | +**Package Purposes** |
| 47 | + |
| 48 | +* _FeatureManagement.Database.Abstractions_ |
| 49 | + * Standard functionalities for managing feature flags across various databases |
| 50 | + |
| 51 | + |
| 52 | +## Getting Started |
| 53 | + |
| 54 | +.NET Feature Management Database allows you to manage feature definitions stored in a database. |
| 55 | +The built-in `DatabaseFeatureDefinitionProvider` retrieves these definitions and converts them into `FeatureDefinition` objects used by the feature management system. |
| 56 | +This setup relies on an implementation of the `IFeatureStore` interface to access the database. |
| 57 | + |
| 58 | +### Entities |
| 59 | + |
| 60 | +Two primary entities are pre-configured for database feature management: |
| 61 | + |
| 62 | +- **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. |
| 63 | + |
| 64 | +- **FeatureSettings**: contains the settings for a feature and these define the conditions under which a feature is enabled. |
| 65 | +The parameters are stored in JSON format and based on Feature Management [built-in feature filter][Feature Management built-in filters] or [contextual feature filter][Feature Management contextual filters] configuration, and can include [custom feature filter][Feature Management custom filters] configuration. |
| 66 | + |
| 67 | +#### Example |
| 68 | + |
| 69 | +Suppose you want to define a feature that is enabled for 50% of the users. |
| 70 | +Here is an example of how you can define such a feature and its settings: |
| 71 | + |
| 72 | +```csharp |
| 73 | +… |
| 74 | +var newFeature = new Feature |
| 75 | +{ |
| 76 | + Id = Guid.NewGuid(), |
| 77 | + Name = "NewFeature", |
| 78 | + RequirementType = RequirementType.Any, |
| 79 | + Settings = new List<FeatureSettings> |
| 80 | + { |
| 81 | + new FeatureSettings |
| 82 | + { |
| 83 | + Id = Guid.NewGuid(), |
| 84 | + FilterType = FeatureFilterType.Percentage, |
| 85 | + Parameters = "{\"Value\":50}" |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | +… |
| 90 | +``` |
| 91 | + |
| 92 | +### Feature Store |
| 93 | + |
| 94 | +The `IFeatureStore` interface is the core abstraction for retrieving feature data from a database. |
| 95 | +Implement this interface to connect to your specific database (e.g., SQL Server, MongoDB, etc.). |
| 96 | + |
| 97 | +```csharp |
| 98 | +public class MyFeatureStore : IFeatureStore |
| 99 | +{ |
| 100 | + // Implementation to fetch feature definitions from your database |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +### Service Registration |
| 105 | + |
| 106 | +Database feature management relies on .NET Core dependency injection. |
| 107 | +Registering the feature management services can be done using the following approaches: |
| 108 | + |
| 109 | +* #### Register Feature Store and Feature Management Separately |
| 110 | + |
| 111 | + First, register your custom `IFeatureStore` implementation and then add database feature management: |
| 112 | + |
| 113 | + ```csharp |
| 114 | + services.AddFeatureStore<MyFeatureStore>(); |
| 115 | + services.AddDatabaseFeatureManagement(); |
| 116 | + ``` |
| 117 | + |
| 118 | + This approach allows for more modular and explicit registration of services. |
| 119 | + |
| 120 | +* #### Register Feature Store and Feature Management in a Single Call |
| 121 | + |
| 122 | + For a more streamlined setup, you can register your custom `IFeatureStore` and add database feature management in one step: |
| 123 | + |
| 124 | + ```csharp |
| 125 | + services.AddDatabaseFeatureManagement<MyFeatureStore>(); |
| 126 | + ``` |
| 127 | + |
| 128 | + This method simplifies the configuration by combining both registrations into a single call. |
| 129 | + |
| 130 | +> [!NOTE] |
| 131 | +> In the context of database solutions, the feature management services will be added as scoped services. |
| 132 | + |
| 133 | +> [!IMPORTANT] |
| 134 | +> To use database feature management, you need to register an **IFeatureStore**. |
| 135 | + |
| 136 | +### Configure Cache |
| 137 | + |
| 138 | +To improve performance and reduce database load, you can configure caching for the feature store. |
| 139 | +The `WithCacheService` method provides several ways to configure caching: |
| 140 | + |
| 141 | +* #### Using Default Options |
| 142 | + |
| 143 | + To register the cache service with default options: |
| 144 | + |
| 145 | + ```csharp |
| 146 | + services.AddDatabaseFeatureManagement<MyFeatureStore>() |
| 147 | + .WithCacheService(); |
| 148 | + ``` |
| 149 | + |
| 150 | + > By default, the inactive cache will be removed after 30 minutes. |
| 151 | + |
| 152 | +* #### Using Custom Configuration Action |
| 153 | + |
| 154 | + To register the cache service with custom options: |
| 155 | + |
| 156 | + ```csharp |
| 157 | + services.AddDatabaseFeatureManagement<MyFeatureStore>() |
| 158 | + .WithCacheService(options => |
| 159 | + { |
| 160 | + options.SlidingExpiration = TimeSpan.FromMinutes(10); |
| 161 | + options.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1); |
| 162 | + }); |
| 163 | + ``` |
| 164 | + |
| 165 | +* #### Using Configuration Section |
| 166 | + |
| 167 | + To register the cache service using settings from a configuration section: |
| 168 | + |
| 169 | + ```csharp |
| 170 | + var cacheConfiguration = Configuration.GetSection(FeatureCacheOptions.Name); |
| 171 | + services.AddDatabaseFeatureManagement<MyFeatureStore>() |
| 172 | + .WithCacheService(cacheConfiguration); |
| 173 | + ``` |
| 174 | + And in your `appsettings.json`: |
| 175 | + |
| 176 | + ```json |
| 177 | + { |
| 178 | + "FeatureCacheOptions": { |
| 179 | + "AbsoluteExpirationRelativeToNow": "01:00:00", |
| 180 | + "SlidingExpiration": "00:30:00" |
| 181 | + } |
| 182 | + } |
| 183 | + ``` |
| 184 | + |
| 185 | +#### Advanced Cache Configuration |
| 186 | + |
| 187 | +The cache keys have a prefix defined in the options (`FeatureCacheOptions.CachePrefix`). |
| 188 | + |
| 189 | +- For a single feature, the default cache key will be the name of that feature (prefix included). |
| 190 | + Example: |
| 191 | + |
| 192 | + ```text |
| 193 | + MyFeature => FMDb_MyFeature |
| 194 | + ``` |
| 195 | + |
| 196 | +- For all features, the default cache key will be "features" combined with the prefix: |
| 197 | + |
| 198 | + ```text |
| 199 | + All features => FMDb_features |
| 200 | + ``` |
| 201 | + |
| 202 | + That _"features"_ can be overridden using one of the methods above. So you can have `"FMDb_your-own-cache-key"`. |
| 203 | + |
| 204 | +See the `FeatureCacheOptions` class for more cache-related settings. |
| 205 | + |
| 206 | +> [!WARNING] |
| 207 | +> When a feature value is updated in the database, the cache does not automatically clean up or refresh. |
| 208 | +> Ensure to handle cache invalidation appropriately in such scenarios to keep the cache in sync with the database. |
| 209 | + |
| 210 | + |
| 211 | +## Consumption |
| 212 | + |
| 213 | +The basic form of feature management is checking if a feature flag is enabled and then performing actions based on the result. |
| 214 | +This is done through the `IFeatureManager`'s `IsEnabledAsync` method. |
| 215 | + |
| 216 | +```csharp |
| 217 | +… |
| 218 | +IFeatureManager featureManager; |
| 219 | +… |
| 220 | +if (await featureManager.IsEnabledAsync("MyFeature")) |
| 221 | +{ |
| 222 | + // Do something |
| 223 | +} |
| 224 | +``` |
| 225 | + |
| 226 | +See more [here][Feature Management Consumption]. |
| 227 | + |
| 228 | +### ASP.NET Core Integration |
| 229 | + |
| 230 | +The database feature management library provides support in ASP.NET Core and MVC to enable common feature flag scenarios in web applications. |
| 231 | + |
| 232 | +See more [here][Feature Management ASP.NET Core]. |
| 233 | + |
| 234 | + |
| 235 | +## Contributing |
| 236 | + |
| 237 | +Please see our [Contribution Guidelines](CONTRIBUTING.md) for more information. |
| 238 | + |
| 239 | + |
| 240 | +[Feature Management]: https://github.com/microsoft/FeatureManagement-Dotnet |
| 241 | +[Feature Management built-in filters]: https://github.com/microsoft/FeatureManagement-Dotnet?tab=readme-ov-file#built-in-feature-filters |
| 242 | +[Feature Management contextual filters]: https://github.com/microsoft/FeatureManagement-Dotnet?tab=readme-ov-file#contextual-feature-filters |
| 243 | +[Feature Management custom filters]: https://github.com/microsoft/FeatureManagement-Dotnet?tab=readme-ov-file#implementing-a-feature-filter |
| 244 | +[Feature Management Consumption]: https://github.com/microsoft/FeatureManagement-Dotnet?tab=readme-ov-file#consumption |
| 245 | +[Feature Management ASP.NET Core]: https://github.com/microsoft/FeatureManagement-Dotnet?tab=readme-ov-file#aspnet-core-integration |
0 commit comments