From cda6d6f82e8566e060c296138640fb22acd43426 Mon Sep 17 00:00:00 2001 From: Antonis Kotis Date: Fri, 9 May 2025 20:18:15 +0300 Subject: [PATCH] Refactor BaseUnitTest to improve dependency injection setup - Refactored AddScoped dependency Injection. - Ensured proper configuration loading from `appsettings.json`. - Improved code readability and maintainability by organizing internal fields and private methods. --- FeatureMasterX.XUnitTest/BaseUnitTest.cs | 4 +++ FeatureMasterX/FeatureMasterX.csproj | 4 ++- FeatureMasterX/FeatureMasterXExtensions.cs | 32 +++++++++++++++++++ .../Filters/ListCheckFeatureFilter.cs | 8 ++--- .../Filters/ListCheckFeatureFilterProvider.cs | 15 ++++++--- 5 files changed, 54 insertions(+), 9 deletions(-) diff --git a/FeatureMasterX.XUnitTest/BaseUnitTest.cs b/FeatureMasterX.XUnitTest/BaseUnitTest.cs index 44f9e68..2691c7e 100644 --- a/FeatureMasterX.XUnitTest/BaseUnitTest.cs +++ b/FeatureMasterX.XUnitTest/BaseUnitTest.cs @@ -23,6 +23,10 @@ public abstract class BaseUnitTest : IAsyncLifetime public Task DisposeAsync() => Task.CompletedTask; + /// + /// Initialize the test class. + /// + /// public async Task InitializeAsync() { _instance = CreateServiceCollection(); diff --git a/FeatureMasterX/FeatureMasterX.csproj b/FeatureMasterX/FeatureMasterX.csproj index b44ea51..c862452 100644 --- a/FeatureMasterX/FeatureMasterX.csproj +++ b/FeatureMasterX/FeatureMasterX.csproj @@ -14,10 +14,12 @@ https://github.com/SkJonko/FeatureMasterX FeatureMasterX A powerful and easy-to-use extension for Microsoft's FeatureManagement library - 9.0.1 + 9.0.2 SKJonko Copyright © 2025 + 9.0.2 + - Feature: Add Scoped Dependency Injection of FeatureMasterX 9.0.1 - Feature: Multitargets 9.0.0 diff --git a/FeatureMasterX/FeatureMasterXExtensions.cs b/FeatureMasterX/FeatureMasterXExtensions.cs index 35a774b..1bbde56 100644 --- a/FeatureMasterX/FeatureMasterXExtensions.cs +++ b/FeatureMasterX/FeatureMasterXExtensions.cs @@ -1,18 +1,35 @@ using FeatureMasterX.Filters; using Microsoft.Extensions.DependencyInjection; using Microsoft.FeatureManagement; +using System.Diagnostics.CodeAnalysis; namespace FeatureMasterX { + /// + /// Extensions for FeatureMasterX. + /// public static class FeatureMasterXExtensions { #region PUBLIC + /// + /// The name of the feature filter that checks if the user is in a list of allowed users. + /// public const string ListCheck = "ListCheck"; + + /// + /// The name of the configuration key that contains the list of allowed users. + /// public const string AllowedUsers = "AllowedUsers"; #endregion + /// + /// Adds the FeatureMasterX services to the specified . + /// + /// + /// + [ExcludeFromCodeCoverage] public static IServiceCollection AddFeatureMasterX(this IServiceCollection services) { services.AddSingleton() @@ -21,5 +38,20 @@ public static IServiceCollection AddFeatureMasterX(this IServiceCollection servi return services; } + + /// + /// Adds the Scoped FeatureMasterX services to the specified . + /// + /// + /// + [ExcludeFromCodeCoverage] + public static IServiceCollection AddScopedFeatureMasterX(this IServiceCollection services) + { + services.AddSingleton() + .AddScopedFeatureManagement() + .AddFeatureFilter(); + + return services; + } } } \ No newline at end of file diff --git a/FeatureMasterX/Filters/ListCheckFeatureFilter.cs b/FeatureMasterX/Filters/ListCheckFeatureFilter.cs index 0f4bf46..52454dc 100644 --- a/FeatureMasterX/Filters/ListCheckFeatureFilter.cs +++ b/FeatureMasterX/Filters/ListCheckFeatureFilter.cs @@ -14,19 +14,19 @@ internal class ListCheckFeatureFilter : IContextualFeatureFilter /// Evaluate the feature filter /// /// - /// + /// The data that stored under the EnabledFor of appsettings. /// - public Task EvaluateAsync(FeatureFilterEvaluationContext context, string userEmail) + public Task EvaluateAsync(FeatureFilterEvaluationContext context, string appsettingsData) { var allowedUsers = JsonSerializer.Deserialize>(context?.Parameters?.GetSection(FeatureMasterXExtensions.AllowedUsers)?.Get()); if (allowedUsers?.Contains("ALL") ?? false) return Task.FromResult(true); - if (string.IsNullOrEmpty(userEmail)) + if (string.IsNullOrEmpty(appsettingsData)) return Task.FromResult(false); - if (allowedUsers?.Contains(userEmail) ?? false) + if (allowedUsers?.Contains(appsettingsData) ?? false) return Task.FromResult(true); return Task.FromResult(false); diff --git a/FeatureMasterX/Filters/ListCheckFeatureFilterProvider.cs b/FeatureMasterX/Filters/ListCheckFeatureFilterProvider.cs index 093a3c9..289846b 100644 --- a/FeatureMasterX/Filters/ListCheckFeatureFilterProvider.cs +++ b/FeatureMasterX/Filters/ListCheckFeatureFilterProvider.cs @@ -4,6 +4,9 @@ namespace FeatureMasterX.Filters { + /// + /// Provides a feature filter that checks if the user is in a list of allowed users. + /// public class ListCheckFeatureFilterProvider : IFeatureDefinitionProvider { #region Private Values @@ -23,6 +26,7 @@ public ListCheckFeatureFilterProvider(IConfiguration configuration) _configuration = configuration; } + /// public Task GetFeatureDefinitionAsync(string featureName) { var section = _configuration.GetSection($"FeatureManagement:{FeatureMasterXExtensions.ListCheck}:{featureName}:EnabledFor"); @@ -32,7 +36,7 @@ public Task GetFeatureDefinitionAsync(string featureName) return Task.FromResult(new FeatureDefinition { Name = featureName, - EnabledFor = allowedUsers.Select(user => new FeatureFilterConfiguration + EnabledFor = [.. allowedUsers.Select(user => new FeatureFilterConfiguration { Name = FeatureMasterXExtensions.ListCheck, Parameters = new ConfigurationBuilder() @@ -41,11 +45,14 @@ public Task GetFeatureDefinitionAsync(string featureName) { FeatureMasterXExtensions.AllowedUsers, JsonSerializer.Serialize(allowedUsers) } }) .Build() - }).ToList() + })] }); } + /// +#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously public async IAsyncEnumerable GetAllFeatureDefinitionsAsync() +#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously { var featureManagementSection = _configuration.GetSection("FeatureManagement"); @@ -58,7 +65,7 @@ public async IAsyncEnumerable GetAllFeatureDefinitionsAsync() yield return new FeatureDefinition { Name = feature.Key, - EnabledFor = allowedUsers.Select(user => new FeatureFilterConfiguration + EnabledFor = [.. allowedUsers.Select(user => new FeatureFilterConfiguration { Name = FeatureMasterXExtensions.ListCheck, Parameters = new ConfigurationBuilder() @@ -67,7 +74,7 @@ public async IAsyncEnumerable GetAllFeatureDefinitionsAsync() { FeatureMasterXExtensions.AllowedUsers, JsonSerializer.Serialize(allowedUsers) } }) .Build() - }).ToList() + })] }; } }