-
Notifications
You must be signed in to change notification settings - Fork 127
Example app for paramters object #598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,76 @@ | ||||||
| // Copyright (c) Microsoft Corporation. | ||||||
| // Licensed under the MIT license. | ||||||
| // | ||||||
| using Microsoft.FeatureManagement; | ||||||
| using Microsoft.FeatureManagement.FeatureFilters; | ||||||
| namespace ParameterObjectConsoleApp | ||||||
| { | ||||||
| /// <summary> | ||||||
| /// A custom feature definition provider that supplies targeting filter settings | ||||||
| /// directly using the ParametersObject property, avoiding the need to construct IConfiguration. | ||||||
| /// </summary> | ||||||
| public class InMemoryFeatureDefinitionProvider : IFeatureDefinitionProvider | ||||||
| { | ||||||
| private readonly Dictionary<string, FeatureDefinition> _featureDefinitions; | ||||||
|
|
||||||
| public InMemoryFeatureDefinitionProvider() | ||||||
| { | ||||||
| // | ||||||
| // Define feature flags with targeting settings. | ||||||
| // This demonstrates supplying filter parameters directly via ParametersObject | ||||||
| // instead of constructing an IConfiguration with key-value pairs. | ||||||
| _featureDefinitions = new Dictionary<string, FeatureDefinition> | ||||||
| { | ||||||
| ["Beta"] = new FeatureDefinition | ||||||
| { | ||||||
| Name = "Beta", | ||||||
| EnabledFor = new List<FeatureFilterConfiguration> | ||||||
| { | ||||||
| new FeatureFilterConfiguration | ||||||
| { | ||||||
| Name = "Microsoft.Targeting", | ||||||
| ParametersObject = new TargetingFilterSettings | ||||||
| { | ||||||
| Audience = new Audience | ||||||
| { | ||||||
| Users = new List<string> { "Jeff", "Anne" }, | ||||||
| Groups = new List<GroupRollout> | ||||||
| { | ||||||
| new GroupRollout | ||||||
| { | ||||||
| Name = "Management", | ||||||
| RolloutPercentage = 100 | ||||||
| }, | ||||||
| new GroupRollout | ||||||
| { | ||||||
| Name = "TeamMembers", | ||||||
| RolloutPercentage = 45 | ||||||
| } | ||||||
| }, | ||||||
| DefaultRolloutPercentage = 20 | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| public Task<FeatureDefinition> GetFeatureDefinitionAsync(string featureName) | ||||||
| { | ||||||
| _featureDefinitions.TryGetValue(featureName, out FeatureDefinition definition); | ||||||
|
|
||||||
| return Task.FromResult(definition); | ||||||
| } | ||||||
|
|
||||||
| public async IAsyncEnumerable<FeatureDefinition> GetAllFeatureDefinitionsAsync() | ||||||
| { | ||||||
| foreach (var definition in _featureDefinitions.Values) | ||||||
| { | ||||||
| yield return definition; | ||||||
| } | ||||||
|
|
||||||
| await Task.CompletedTask; | ||||||
|
Comment on lines
+72
to
+73
|
||||||
| await Task.CompletedTask; |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,19 @@ | ||||||||||||||
| <Project Sdk="Microsoft.NET.Sdk"> | ||||||||||||||
|
|
||||||||||||||
| <PropertyGroup> | ||||||||||||||
| <OutputType>Exe</OutputType> | ||||||||||||||
| <TargetFramework>net8.0</TargetFramework> | ||||||||||||||
| <ImplicitUsings>enable</ImplicitUsings> | ||||||||||||||
| </PropertyGroup> | ||||||||||||||
|
|
||||||||||||||
| <ItemGroup> | ||||||||||||||
| <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" /> | ||||||||||||||
| <PackageReference Include="System.Text.Json" Version="8.0.5" /> | ||||||||||||||
| <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" /> | ||||||||||||||
| </ItemGroup> | ||||||||||||||
|
|
||||||||||||||
| <ItemGroup> | ||||||||||||||
|
Comment on lines
+10
to
+15
|
||||||||||||||
| <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" /> | |
| <PackageReference Include="System.Text.Json" Version="8.0.5" /> | |
| <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" /> | |
| </ItemGroup> | |
| <ItemGroup> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
| // | ||
| using Microsoft.FeatureManagement; | ||
| using Microsoft.FeatureManagement.FeatureFilters; | ||
| using ParameterObjectConsoleApp; | ||
|
|
||
| // | ||
| // Create a feature manager using a custom definition provider | ||
| // that supplies targeting filter settings directly via ParametersObject. | ||
| var featureManager = new FeatureManager(new InMemoryFeatureDefinitionProvider()) | ||
| { | ||
| FeatureFilters = new List<IFeatureFilterMetadata> { new ContextualTargetingFilter() } | ||
| }; | ||
|
|
||
| // | ||
| // Simulate evaluating the "Beta" feature for different users | ||
| var users = new List<(string UserId, List<string> Groups)> | ||
| { | ||
| ("Jeff", new List<string> { "TeamMembers" }), // Targeted by user name | ||
| ("Anne", new List<string> { "Management" }), // Targeted by user name | ||
| ("Sam", new List<string> { "Management" }), // Targeted by group (100% rollout) | ||
| ("Alice", new List<string> { "TeamMembers" }), // May be targeted by group (45% rollout) | ||
| ("Bob", new List<string> { "External" }) // Only targeted by default rollout (20%) | ||
| }; | ||
|
|
||
| foreach (var (userId, groups) in users) | ||
| { | ||
| const string FeatureName = "Beta"; | ||
|
|
||
| var targetingContext = new TargetingContext | ||
| { | ||
| UserId = userId, | ||
| Groups = groups | ||
| }; | ||
|
|
||
| bool enabled = await featureManager.IsEnabledAsync(FeatureName, targetingContext); | ||
|
|
||
| Console.WriteLine($"The {FeatureName} feature is {(enabled ? "enabled" : "disabled")} for the user '{userId}'."); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,33 @@ | ||||||
| # ParametersObject Console App | ||||||
|
|
||||||
| This example demonstrates how to use the `ParametersObject` property on `FeatureFilterConfiguration` to supply filter settings directly from a custom `IFeatureDefinitionProvider`. | ||||||
|
|
||||||
| ## Overview | ||||||
|
|
||||||
| When implementing a custom `IFeatureDefinitionProvider` that sources feature definitions from alternative backends (e.g., databases, REST APIs), you no longer need to construct an `IConfiguration` object to pass filter parameters. Instead, you can assign settings like `TargetingFilterSettings` directly to `ParametersObject`. | ||||||
|
|
||||||
| ## Key Concept | ||||||
|
|
||||||
| ```csharp | ||||||
| new FeatureFilterConfiguration | ||||||
| { | ||||||
| Name = "Microsoft.Targeting", | ||||||
| ParametersObject = new TargetingFilterSettings | ||||||
| { | ||||||
| Audience = new Audience | ||||||
| { | ||||||
| Users = new List<string> { "Jeff", "Anne" }, | ||||||
| Groups = new List<GroupRollout> { ... }, | ||||||
| DefaultRolloutPercentage = 20 | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| This eliminates the need for verbose `IConfiguration` construction with magic string keys. | ||||||
|
|
||||||
| ## Running | ||||||
|
|
||||||
| ``` | ||||||
| dotnet run | ||||||
|
||||||
| dotnet run | |
| dotnet run --project examples/ParameterObjectConsoleApp/ParameterObjectConsoleApp.csproj |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The folder/project/namespace name uses
ParameterObject...(singular) while the API being demonstrated isParametersObject(plural). Consider aligning the example name with the public API name to reduce confusion when users search for it.