Skip to content

Commit cbda0c6

Browse files
set feature name in constructor
1 parent df47105 commit cbda0c6

File tree

2 files changed

+14
-28
lines changed

2 files changed

+14
-28
lines changed

src/Microsoft.FeatureManagement/FeatureManagementBuilderExtensions.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,22 @@ public static IFeatureManagementBuilder WithVariantService<TService>(this IFeatu
5656

5757
if (builder.Services.Any(descriptor => descriptor.ServiceType == typeof(IVariantServiceProvider<TService>)))
5858
{
59-
throw new InvalidOperationException($"Variant services of {typeof(TService)} has been added.");
59+
throw new InvalidOperationException($"A variant service of {typeof(TService).FullName} has already been added.");
6060
}
6161

6262
if (builder.Services.Any(descriptor => descriptor.ServiceType == typeof(IFeatureManager) && descriptor.Lifetime == ServiceLifetime.Scoped))
6363
{
6464
builder.Services.AddScoped<IVariantServiceProvider<TService>>(sp => new VariantServiceProvider<TService>(
65-
sp.GetRequiredService<IEnumerable<TService>>(),
66-
sp.GetRequiredService<IVariantFeatureManager>())
67-
{
68-
FeatureName = featureName,
69-
});
65+
featureName,
66+
sp.GetRequiredService<IVariantFeatureManager>(),
67+
sp.GetRequiredService<IEnumerable<TService>>()));
7068
}
7169
else
7270
{
7371
builder.Services.AddSingleton<IVariantServiceProvider<TService>>(sp => new VariantServiceProvider<TService>(
74-
sp.GetRequiredService<IEnumerable<TService>>(),
75-
sp.GetRequiredService<IVariantFeatureManager>())
76-
{
77-
FeatureName = featureName,
78-
});
72+
featureName,
73+
sp.GetRequiredService<IVariantFeatureManager>(),
74+
sp.GetRequiredService<IEnumerable<TService>>()));
7975
}
8076

8177
return builder;

src/Microsoft.FeatureManagement/VariantServiceProvider.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,20 @@ internal class VariantServiceProvider<TService> : IVariantServiceProvider<TServi
2424
/// <summary>
2525
/// Creates a variant service provider.
2626
/// </summary>
27+
/// <param name="featureName">The feature flag that should be used to determine which variant of the service should be used.</param>
28+
/// <param name="featureManager">The feature manager to get the assigned variant of the feature flag.</param>
2729
/// <param name="services">Implementation variants of TService.</param>
28-
/// <param name="featureManager">Feature manager to get the assigned variant of the variant feature flag.</param>
29-
/// <exception cref="ArgumentNullException">Thrown if <paramref name="services"/> is null.</exception>
30+
/// <exception cref="ArgumentNullException">Thrown if <paramref name="featureName"/> is null.</exception>
3031
/// <exception cref="ArgumentNullException">Thrown if <paramref name="featureManager"/> is null.</exception>
31-
public VariantServiceProvider(IEnumerable<TService> services, IVariantFeatureManager featureManager)
32+
/// <exception cref="ArgumentNullException">Thrown if <paramref name="services"/> is null.</exception>
33+
public VariantServiceProvider(string featureName, IVariantFeatureManager featureManager, IEnumerable<TService> services)
3234
{
33-
_services = services ?? throw new ArgumentNullException(nameof(services));
35+
_featureName = featureName ?? throw new ArgumentNullException(nameof(featureName));
3436
_featureManager = featureManager ?? throw new ArgumentNullException(nameof(featureManager));
37+
_services = services ?? throw new ArgumentNullException(nameof(services));
3538
_variantServiceCache = new ConcurrentDictionary<string, TService>();
3639
}
3740

38-
/// <summary>
39-
/// The variant feature flag used to assign variants.
40-
/// </summary>
41-
public string FeatureName
42-
{
43-
get => _featureName;
44-
45-
init
46-
{
47-
_featureName = value ?? throw new ArgumentNullException(nameof(value));
48-
}
49-
}
50-
5141
/// <summary>
5242
/// Gets implementation of TService according to the assigned variant from the feature flag.
5343
/// </summary>

0 commit comments

Comments
 (0)