-
Notifications
You must be signed in to change notification settings - Fork 126
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
Open
linglingye001
wants to merge
2
commits into
main
Choose a base branch
from
linglingye/example-app-parameters-object
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+162
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
examples/ParametersObjectConsoleApp/InMemoryFeatureDefinitionProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ParametersObjectConsoleApp | ||
| { | ||
| /// <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; | ||
| } | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
examples/ParametersObjectConsoleApp/ParametersObjectConsoleApp.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\src\Microsoft.FeatureManagement\Microsoft.FeatureManagement.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ParametersObjectConsoleApp; | ||
|
|
||
| // | ||
| // 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}'."); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 --project examples/ParametersObjectConsoleApp/ParametersObjectConsoleApp.csproj | ||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
GetAllFeatureDefinitionsAsyncis implemented as an async iterator but only awaitsTask.CompletedTaskat the end, which is redundant and adds noise. Consider removing the trailing await (and/or droppingasyncif you switch to a non-async implementation) so the method is a straightforward async iterator.