Skip to content

Commit aeed650

Browse files
Do not use DI for console app example (#528)
* not use DI for console app example * update console targeting example * simlify example
1 parent 8708158 commit aeed650

File tree

2 files changed

+51
-69
lines changed

2 files changed

+51
-69
lines changed

examples/ConsoleApp/Program.cs

+22-33
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT license.
33
//
44
using Microsoft.Extensions.Configuration;
5-
using Microsoft.Extensions.DependencyInjection;
65
using Microsoft.FeatureManagement;
76

87
//
@@ -11,45 +10,35 @@
1110
.AddJsonFile("appsettings.json")
1211
.Build();
1312

14-
//
15-
// Setup application services + feature management
16-
IServiceCollection services = new ServiceCollection();
13+
var featureManager = new FeatureManager(new ConfigurationFeatureDefinitionProvider(configuration))
14+
{
15+
FeatureFilters = new List<IFeatureFilterMetadata> { new AccountIdFilter() }
16+
};
1717

18-
services.AddSingleton(configuration)
19-
.AddFeatureManagement()
20-
.AddFeatureFilter<AccountIdFilter>();
18+
var accounts = new List<string>()
19+
{
20+
"abc",
21+
"adef",
22+
"abcdefghijklmnopqrstuvwxyz"
23+
};
2124

2225
//
23-
// Get the feature manager from application services
24-
using (ServiceProvider serviceProvider = services.BuildServiceProvider())
26+
// Mimic work items in a task-driven console application
27+
foreach (var account in accounts)
2528
{
26-
IFeatureManager featureManager = serviceProvider.GetRequiredService<IFeatureManager>();
27-
28-
var accounts = new List<string>()
29-
{
30-
"abc",
31-
"adef",
32-
"abcdefghijklmnopqrstuvwxyz"
33-
};
29+
const string FeatureName = "Beta";
3430

3531
//
36-
// Mimic work items in a task-driven console application
37-
foreach (var account in accounts)
32+
// Check if feature enabled
33+
//
34+
var accountServiceContext = new AccountServiceContext
3835
{
39-
const string FeatureName = "Beta";
40-
41-
//
42-
// Check if feature enabled
43-
//
44-
var accountServiceContext = new AccountServiceContext
45-
{
46-
AccountId = account
47-
};
36+
AccountId = account
37+
};
4838

49-
bool enabled = await featureManager.IsEnabledAsync(FeatureName, accountServiceContext);
39+
bool enabled = await featureManager.IsEnabledAsync(FeatureName, accountServiceContext);
5040

51-
//
52-
// Output results
53-
Console.WriteLine($"The {FeatureName} feature is {(enabled ? "enabled" : "disabled")} for the '{account}' account.");
54-
}
41+
//
42+
// Output results
43+
Console.WriteLine($"The {FeatureName} feature is {(enabled ? "enabled" : "disabled")} for the '{account}' account.");
5544
}
+29-36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using Microsoft.Extensions.Configuration;
2-
using Microsoft.Extensions.DependencyInjection;
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
//
4+
using Microsoft.Extensions.Configuration;
35
using Microsoft.FeatureManagement;
46
using Microsoft.FeatureManagement.FeatureFilters;
57
using TargetingConsoleApp.Identity;
@@ -10,48 +12,39 @@
1012
.AddJsonFile("appsettings.json")
1113
.Build();
1214

13-
//
14-
// Setup application services + feature management
15-
IServiceCollection services = new ServiceCollection();
16-
17-
services.AddSingleton(configuration)
18-
.AddFeatureManagement();
15+
var featureManager = new FeatureManager(new ConfigurationFeatureDefinitionProvider(configuration))
16+
{
17+
FeatureFilters = new List<IFeatureFilterMetadata> { new ContextualTargetingFilter() }
18+
};
1919

2020
var userRepository = new InMemoryUserRepository();
2121

2222
//
23-
// Get the feature manager from application services
24-
using (ServiceProvider serviceProvider = services.BuildServiceProvider())
23+
// We'll simulate a task to run on behalf of each known user
24+
// To do this we enumerate all the users in our user repository
25+
IEnumerable<string> userIds = InMemoryUserRepository.Users.Select(u => u.Id);
26+
27+
//
28+
// Mimic work items in a task-driven console application
29+
foreach (string userId in userIds)
2530
{
26-
IFeatureManager featureManager = serviceProvider.GetRequiredService<IFeatureManager>();
31+
const string FeatureName = "Beta";
2732

2833
//
29-
// We'll simulate a task to run on behalf of each known user
30-
// To do this we enumerate all the users in our user repository
31-
IEnumerable<string> userIds = InMemoryUserRepository.Users.Select(u => u.Id);
34+
// Get user
35+
User user = await userRepository.GetUser(userId);
3236

3337
//
34-
// Mimic work items in a task-driven console application
35-
foreach (string userId in userIds)
38+
// Check if feature enabled
39+
var targetingContext = new TargetingContext
3640
{
37-
const string FeatureName = "Beta";
38-
39-
//
40-
// Get user
41-
User user = await userRepository.GetUser(userId);
42-
43-
//
44-
// Check if feature enabled
45-
var targetingContext = new TargetingContext
46-
{
47-
UserId = user.Id,
48-
Groups = user.Groups
49-
};
50-
51-
bool enabled = await featureManager.IsEnabledAsync(FeatureName, targetingContext);
52-
53-
//
54-
// Output results
55-
Console.WriteLine($"The {FeatureName} feature is {(enabled ? "enabled" : "disabled")} for the user '{userId}'.");
56-
}
41+
UserId = user.Id,
42+
Groups = user.Groups
43+
};
44+
45+
bool enabled = await featureManager.IsEnabledAsync(FeatureName, targetingContext);
46+
47+
//
48+
// Output results
49+
Console.WriteLine($"The {FeatureName} feature is {(enabled ? "enabled" : "disabled")} for the user '{userId}'.");
5750
}

0 commit comments

Comments
 (0)