Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link

Copilot AI Apr 22, 2026

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 is ParametersObject (plural). Consider aligning the example name with the public API name to reduce confusion when users search for it.

Suggested change
namespace ParameterObjectConsoleApp
namespace ParametersObjectConsoleApp

Copilot uses AI. Check for mistakes.
{
/// <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
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetAllFeatureDefinitionsAsync is implemented as an async iterator but only awaits Task.CompletedTask at the end, which is redundant and adds noise. Consider removing the trailing await (and/or dropping async if you switch to a non-async implementation) so the method is a straightforward async iterator.

Suggested change
await Task.CompletedTask;

Copilot uses AI. Check for mistakes.
}
}
}
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
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The project references configuration/JSON/DI packages, but this example doesn't use IConfiguration, JSON serialization, or DI. Consider removing these PackageReferences to keep the sample minimal (the FeatureManagement project reference already brings in what it needs transitively).

Suggested change
<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>

Copilot uses AI. Check for mistakes.
<ProjectReference Include="..\..\src\Microsoft.FeatureManagement\Microsoft.FeatureManagement.csproj" />
</ItemGroup>

</Project>
40 changes: 40 additions & 0 deletions examples/ParameterObjectConsoleApp/Program.cs
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}'.");
}
33 changes: 33 additions & 0 deletions examples/ParameterObjectConsoleApp/README.md
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
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The running instructions are ambiguous from the repo root (plain dotnet run will likely execute a different project or error). Consider updating this section to either include a cd examples/ParameterObjectConsoleApp step or use dotnet run --project examples/ParameterObjectConsoleApp/ParameterObjectConsoleApp.csproj.

Suggested change
dotnet run
dotnet run --project examples/ParameterObjectConsoleApp/ParameterObjectConsoleApp.csproj

Copilot uses AI. Check for mistakes.
```
Loading