diff --git a/examples/ConsoleApp/Program.cs b/examples/ConsoleApp/Program.cs index fb43a50e..ab3249da 100644 --- a/examples/ConsoleApp/Program.cs +++ b/examples/ConsoleApp/Program.cs @@ -2,7 +2,6 @@ // Licensed under the MIT license. // using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; using Microsoft.FeatureManagement; // @@ -11,45 +10,35 @@ .AddJsonFile("appsettings.json") .Build(); -// -// Setup application services + feature management -IServiceCollection services = new ServiceCollection(); +var featureManager = new FeatureManager(new ConfigurationFeatureDefinitionProvider(configuration)) +{ + FeatureFilters = new List { new AccountIdFilter() } +}; -services.AddSingleton(configuration) - .AddFeatureManagement() - .AddFeatureFilter(); +var accounts = new List() +{ + "abc", + "adef", + "abcdefghijklmnopqrstuvwxyz" +}; // -// Get the feature manager from application services -using (ServiceProvider serviceProvider = services.BuildServiceProvider()) +// Mimic work items in a task-driven console application +foreach (var account in accounts) { - IFeatureManager featureManager = serviceProvider.GetRequiredService(); - - var accounts = new List() - { - "abc", - "adef", - "abcdefghijklmnopqrstuvwxyz" - }; + const string FeatureName = "Beta"; // - // Mimic work items in a task-driven console application - foreach (var account in accounts) + // Check if feature enabled + // + var accountServiceContext = new AccountServiceContext { - const string FeatureName = "Beta"; - - // - // Check if feature enabled - // - var accountServiceContext = new AccountServiceContext - { - AccountId = account - }; + AccountId = account + }; - bool enabled = await featureManager.IsEnabledAsync(FeatureName, accountServiceContext); + bool enabled = await featureManager.IsEnabledAsync(FeatureName, accountServiceContext); - // - // Output results - Console.WriteLine($"The {FeatureName} feature is {(enabled ? "enabled" : "disabled")} for the '{account}' account."); - } + // + // Output results + Console.WriteLine($"The {FeatureName} feature is {(enabled ? "enabled" : "disabled")} for the '{account}' account."); } diff --git a/examples/TargetingConsoleApp/Program.cs b/examples/TargetingConsoleApp/Program.cs index 02c17542..ce944300 100644 --- a/examples/TargetingConsoleApp/Program.cs +++ b/examples/TargetingConsoleApp/Program.cs @@ -1,5 +1,7 @@ -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. +// +using Microsoft.Extensions.Configuration; using Microsoft.FeatureManagement; using Microsoft.FeatureManagement.FeatureFilters; using TargetingConsoleApp.Identity; @@ -10,48 +12,39 @@ .AddJsonFile("appsettings.json") .Build(); -// -// Setup application services + feature management -IServiceCollection services = new ServiceCollection(); - -services.AddSingleton(configuration) - .AddFeatureManagement(); +var featureManager = new FeatureManager(new ConfigurationFeatureDefinitionProvider(configuration)) +{ + FeatureFilters = new List { new ContextualTargetingFilter() } +}; var userRepository = new InMemoryUserRepository(); // -// Get the feature manager from application services -using (ServiceProvider serviceProvider = services.BuildServiceProvider()) +// We'll simulate a task to run on behalf of each known user +// To do this we enumerate all the users in our user repository +IEnumerable userIds = InMemoryUserRepository.Users.Select(u => u.Id); + +// +// Mimic work items in a task-driven console application +foreach (string userId in userIds) { - IFeatureManager featureManager = serviceProvider.GetRequiredService(); + const string FeatureName = "Beta"; // - // We'll simulate a task to run on behalf of each known user - // To do this we enumerate all the users in our user repository - IEnumerable userIds = InMemoryUserRepository.Users.Select(u => u.Id); + // Get user + User user = await userRepository.GetUser(userId); // - // Mimic work items in a task-driven console application - foreach (string userId in userIds) + // Check if feature enabled + var targetingContext = new TargetingContext { - const string FeatureName = "Beta"; - - // - // Get user - User user = await userRepository.GetUser(userId); - - // - // Check if feature enabled - var targetingContext = new TargetingContext - { - UserId = user.Id, - Groups = user.Groups - }; - - bool enabled = await featureManager.IsEnabledAsync(FeatureName, targetingContext); - - // - // Output results - Console.WriteLine($"The {FeatureName} feature is {(enabled ? "enabled" : "disabled")} for the user '{userId}'."); - } + UserId = user.Id, + Groups = user.Groups + }; + + bool enabled = await featureManager.IsEnabledAsync(FeatureName, targetingContext); + + // + // Output results + Console.WriteLine($"The {FeatureName} feature is {(enabled ? "enabled" : "disabled")} for the user '{userId}'."); }