From 3b19e325993b077589a707db0087ddac9d6cf9a7 Mon Sep 17 00:00:00 2001 From: Zhiyuan Liang Date: Fri, 2 Feb 2024 16:04:49 +0800 Subject: [PATCH 1/2] add new constructor --- .../FeatureManager.cs | 30 +++++++++++++++++++ .../ServiceCollectionExtensions.cs | 8 ++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.FeatureManagement/FeatureManager.cs b/src/Microsoft.FeatureManagement/FeatureManager.cs index 55dde3f5..b0ab483a 100644 --- a/src/Microsoft.FeatureManagement/FeatureManager.cs +++ b/src/Microsoft.FeatureManagement/FeatureManager.cs @@ -34,6 +34,22 @@ private class ConfigurationCacheItem public object Settings { get; set; } } + /// + /// Creates a feature manager. + /// + /// The provider of feature flag definitions. + /// Thrown if is null. + /// Thrown if is null. + public FeatureManager(IFeatureDefinitionProvider featureDefinitionProvider) + { + _filterMetadataCache = new ConcurrentDictionary(); + _contextualFeatureFilterCache = new ConcurrentDictionary(); + _featureDefinitionProvider = featureDefinitionProvider ?? throw new ArgumentNullException(nameof(featureDefinitionProvider)); + _options = new FeatureManagementOptions(); + _featureFilters = Enumerable.Empty(); + _sessionManagers = Enumerable.Empty(); + } + /// /// Creates a feature manager. /// @@ -53,6 +69,20 @@ public FeatureManager( _sessionManagers = Enumerable.Empty(); } + /// + /// Options controlling the behavior of the feature manager. + /// + /// Thrown if it is set to null. + public FeatureManagementOptions Options + { + get => _options; + + init + { + _options = value ?? throw new ArgumentNullException(nameof(value)); + } + } + /// /// The collection of feature filter metadata. /// diff --git a/src/Microsoft.FeatureManagement/ServiceCollectionExtensions.cs b/src/Microsoft.FeatureManagement/ServiceCollectionExtensions.cs index 02225d2c..63c8ecf6 100644 --- a/src/Microsoft.FeatureManagement/ServiceCollectionExtensions.cs +++ b/src/Microsoft.FeatureManagement/ServiceCollectionExtensions.cs @@ -43,9 +43,9 @@ public static IFeatureManagementBuilder AddFeatureManagement(this IServiceCollec services.TryAddSingleton(); services.AddSingleton(sp => new FeatureManager( - sp.GetRequiredService(), - sp.GetRequiredService>().Value) + sp.GetRequiredService()) { + Options = sp.GetRequiredService>().Value, FeatureFilters = sp.GetRequiredService>(), SessionManagers = sp.GetRequiredService>(), Cache = sp.GetRequiredService(), @@ -115,9 +115,9 @@ public static IFeatureManagementBuilder AddScopedFeatureManagement(this IService services.TryAddSingleton(); services.AddScoped(sp => new FeatureManager( - sp.GetRequiredService(), - sp.GetRequiredService>().Value) + sp.GetRequiredService()) { + Options = sp.GetRequiredService>().Value, FeatureFilters = sp.GetRequiredService>(), SessionManagers = sp.GetRequiredService>(), Cache = sp.GetRequiredService(), From 0bd773fa16097361eeae95762deccbee34fa4790 Mon Sep 17 00:00:00 2001 From: Zhiyuan Liang Date: Mon, 5 Feb 2024 16:19:07 +0800 Subject: [PATCH 2/2] update --- .../FeatureManager.cs | 35 ++----------------- .../ServiceCollectionExtensions.cs | 8 ++--- 2 files changed, 6 insertions(+), 37 deletions(-) diff --git a/src/Microsoft.FeatureManagement/FeatureManager.cs b/src/Microsoft.FeatureManagement/FeatureManager.cs index b0ab483a..ef850a03 100644 --- a/src/Microsoft.FeatureManagement/FeatureManager.cs +++ b/src/Microsoft.FeatureManagement/FeatureManager.cs @@ -34,55 +34,24 @@ private class ConfigurationCacheItem public object Settings { get; set; } } - /// - /// Creates a feature manager. - /// - /// The provider of feature flag definitions. - /// Thrown if is null. - /// Thrown if is null. - public FeatureManager(IFeatureDefinitionProvider featureDefinitionProvider) - { - _filterMetadataCache = new ConcurrentDictionary(); - _contextualFeatureFilterCache = new ConcurrentDictionary(); - _featureDefinitionProvider = featureDefinitionProvider ?? throw new ArgumentNullException(nameof(featureDefinitionProvider)); - _options = new FeatureManagementOptions(); - _featureFilters = Enumerable.Empty(); - _sessionManagers = Enumerable.Empty(); - } - /// /// Creates a feature manager. /// /// The provider of feature flag definitions. /// Options controlling the behavior of the feature manager. /// Thrown if is null. - /// Thrown if is null. public FeatureManager( IFeatureDefinitionProvider featureDefinitionProvider, - FeatureManagementOptions options) + FeatureManagementOptions options = null) { _filterMetadataCache = new ConcurrentDictionary(); _contextualFeatureFilterCache = new ConcurrentDictionary(); _featureDefinitionProvider = featureDefinitionProvider ?? throw new ArgumentNullException(nameof(featureDefinitionProvider)); - _options = options ?? throw new ArgumentNullException(nameof(options)); + _options = options ?? new FeatureManagementOptions(); _featureFilters = Enumerable.Empty(); _sessionManagers = Enumerable.Empty(); } - /// - /// Options controlling the behavior of the feature manager. - /// - /// Thrown if it is set to null. - public FeatureManagementOptions Options - { - get => _options; - - init - { - _options = value ?? throw new ArgumentNullException(nameof(value)); - } - } - /// /// The collection of feature filter metadata. /// diff --git a/src/Microsoft.FeatureManagement/ServiceCollectionExtensions.cs b/src/Microsoft.FeatureManagement/ServiceCollectionExtensions.cs index 63c8ecf6..02225d2c 100644 --- a/src/Microsoft.FeatureManagement/ServiceCollectionExtensions.cs +++ b/src/Microsoft.FeatureManagement/ServiceCollectionExtensions.cs @@ -43,9 +43,9 @@ public static IFeatureManagementBuilder AddFeatureManagement(this IServiceCollec services.TryAddSingleton(); services.AddSingleton(sp => new FeatureManager( - sp.GetRequiredService()) + sp.GetRequiredService(), + sp.GetRequiredService>().Value) { - Options = sp.GetRequiredService>().Value, FeatureFilters = sp.GetRequiredService>(), SessionManagers = sp.GetRequiredService>(), Cache = sp.GetRequiredService(), @@ -115,9 +115,9 @@ public static IFeatureManagementBuilder AddScopedFeatureManagement(this IService services.TryAddSingleton(); services.AddScoped(sp => new FeatureManager( - sp.GetRequiredService()) + sp.GetRequiredService(), + sp.GetRequiredService>().Value) { - Options = sp.GetRequiredService>().Value, FeatureFilters = sp.GetRequiredService>(), SessionManagers = sp.GetRequiredService>(), Cache = sp.GetRequiredService(),