Skip to content

Commit 176cf78

Browse files
update to the latest design
1 parent 0d0f3f3 commit 176cf78

File tree

6 files changed

+46
-69
lines changed

6 files changed

+46
-69
lines changed

examples/FeatureBasedInjectionPOC/AlgorithmOmega.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class AlgorithmOmega : IAlgorithm
77
{
88
public string Name { get; set; }
99

10-
public AlgorithmOmega()
10+
public AlgorithmOmega(string name)
1111
{
12-
Name = "Omega";
12+
Name = name;
1313
}
1414
}
1515
}

examples/FeatureBasedInjectionPOC/Program.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111

1212
IServiceCollection services = new ServiceCollection();
1313

14+
services.AddSingleton<IAlgorithm, AlgorithmAlpha>();
15+
services.AddSingleton<IAlgorithm, AlgorithmBeta>();
16+
services.AddSingleton<IAlgorithm, AlgorithmSigma>();
17+
services.AddSingleton<IAlgorithm>(sp => new AlgorithmOmega("Omega"));
18+
1419
services.AddSingleton(configuration)
1520
.AddFeatureManagement()
16-
.AddFeatureFilter<TargetingFilter>();
17-
18-
services.AddSingletonForFeature<IAlgorithm>("MyFeature");
21+
.AddFeatureFilter<TargetingFilter>()
22+
.AddFeaturedService<IAlgorithm>("MyFeature");
1923

2024
var targetingContextAccessor = new OnDemandTargetingContextAccessor();
2125

src/Microsoft.FeatureManagement/FeatureManagementBuilderExtensions.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System;
99
using System.Collections.Generic;
1010
using System.Linq;
11+
using System.Reflection;
1112

1213
namespace Microsoft.FeatureManagement
1314
{
@@ -16,6 +17,32 @@ namespace Microsoft.FeatureManagement
1617
/// </summary>
1718
public static class FeatureManagementBuilderExtensions
1819
{
20+
/// <summary>
21+
/// Adds a <see cref="FeaturedService{TService}"/> to the feature management system.
22+
/// </summary>
23+
/// <param name="builder">The <see cref="IFeatureManagementBuilder"/> used to customize feature management functionality.</param>
24+
/// <param name="featureName">The variant feature flag used to assign variants. The <see cref="FeaturedService{TService}"/> will return different implementations of TService according to the assigned variant.</param>
25+
/// <returns>A <see cref="IFeatureManagementBuilder"/> that can be used to customize feature management functionality.</returns>
26+
public static IFeatureManagementBuilder AddFeaturedService<TService>(this IFeatureManagementBuilder builder, string featureName) where TService : class
27+
{
28+
if (builder.Services.Any(descriptor => descriptor.ServiceType == typeof(IFeatureManager) && descriptor.Lifetime == ServiceLifetime.Scoped))
29+
{
30+
builder.Services.AddScoped<IFeaturedService<TService>>(sp => new FeaturedService<TService>(
31+
featureName,
32+
sp.GetRequiredService<IEnumerable<TService>>(),
33+
sp.GetRequiredService<IVariantFeatureManager>()));
34+
}
35+
else
36+
{
37+
builder.Services.AddSingleton<IFeaturedService<TService>>(sp => new FeaturedService<TService>(
38+
featureName,
39+
sp.GetRequiredService<IEnumerable<TService>>(),
40+
sp.GetRequiredService<IVariantFeatureManager>()));
41+
}
42+
43+
return builder;
44+
}
45+
1946
/// <summary>
2047
/// Adds a telemetry publisher to the feature management system.
2148
/// </summary>

src/Microsoft.FeatureManagement/FeaturedService.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,32 @@ namespace Microsoft.FeatureManagement
1212
{
1313
internal class FeaturedService<TService> : IFeaturedService<TService> where TService : class
1414
{
15-
private string _featureName;
16-
private IVariantFeatureManager _featureManager;
17-
private IEnumerable<FeaturedServiceImplementationWrapper<TService>> _services;
15+
private readonly string _featureName;
16+
private readonly IVariantFeatureManager _featureManager;
17+
private readonly IEnumerable<TService> _services;
1818

19-
public FeaturedService(string featureName, IEnumerable<FeaturedServiceImplementationWrapper<TService>> services, IVariantFeatureManager featureManager)
19+
public FeaturedService(string featureName, IEnumerable<TService> services, IVariantFeatureManager featureManager)
2020
{
2121
_featureName = featureName;
22-
_services = services.Where(s => s.FeatureName.Equals(featureName));
22+
_services = services;
2323
_featureManager = featureManager;
2424
}
2525

2626
public async ValueTask<TService> GetAsync(CancellationToken cancellationToken)
2727
{
2828
Variant variant = await _featureManager.GetVariantAsync(_featureName, cancellationToken);
2929

30+
TService implementation = null;
31+
3032
if (variant != null)
3133
{
32-
FeaturedServiceImplementationWrapper<TService> implementationWrapper = _services.FirstOrDefault(s =>
34+
implementation = _services.FirstOrDefault(service =>
3335
IsMatchingVariant(
34-
s.Implementation.GetType(),
36+
service.GetType(),
3537
variant));
36-
37-
if (implementationWrapper != null)
38-
{
39-
return implementationWrapper.Implementation;
40-
}
4138
}
4239

43-
return null;
40+
return implementation;
4441
}
4542

4643
private bool IsMatchingVariant(Type implementationType, Variant variant)

src/Microsoft.FeatureManagement/FeaturedServiceImplementationWrapper.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Microsoft.FeatureManagement/ServiceCollectionExtensions.cs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -194,44 +194,5 @@ public static IFeatureManagementBuilder AddScopedFeatureManagement(this IService
194194

195195
return services.AddScopedFeatureManagement();
196196
}
197-
198-
public static IServiceCollection AddSingletonForFeature<TService>(this IServiceCollection services, string featureName) where TService : class
199-
{
200-
IEnumerable<Type> implementationTypes = Assembly.GetAssembly(typeof(TService))
201-
.GetTypes()
202-
.Where(type =>
203-
typeof(TService).IsAssignableFrom(type) &&
204-
!type.IsInterface &&
205-
!type.IsAbstract);
206-
207-
foreach (var implementationType in implementationTypes)
208-
{
209-
services.TryAddSingleton(implementationType);
210-
211-
services.AddSingleton(sp => new FeaturedServiceImplementationWrapper<TService>()
212-
{
213-
FeatureName = featureName,
214-
Implementation = (TService) sp.GetRequiredService(implementationType)
215-
});
216-
}
217-
218-
219-
if (services.Any(descriptor => descriptor.ServiceType == typeof(IFeatureManager) && descriptor.Lifetime == ServiceLifetime.Scoped))
220-
{
221-
services.AddScoped<IFeaturedService<TService>>(sp => new FeaturedService<TService>(
222-
featureName,
223-
sp.GetRequiredService<IEnumerable<FeaturedServiceImplementationWrapper<TService>>>(),
224-
sp.GetRequiredService<IVariantFeatureManager>()));
225-
}
226-
else
227-
{
228-
services.AddSingleton<IFeaturedService<TService>>(sp => new FeaturedService<TService>(
229-
featureName,
230-
sp.GetRequiredService<IEnumerable<FeaturedServiceImplementationWrapper<TService>>>(),
231-
sp.GetRequiredService<IVariantFeatureManager>()));
232-
}
233-
234-
return services;
235-
}
236197
}
237198
}

0 commit comments

Comments
 (0)