diff --git a/ManagedCode.FeatureChecker/Feature.cs b/ManagedCode.FeatureChecker/Feature.cs deleted file mode 100644 index bb59474..0000000 --- a/ManagedCode.FeatureChecker/Feature.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace ManagedCode.FeatureChecker; -public class Feature -{ - public string Name { get; set; } = null!; - public FeatureStatus Status { get; set; } -} diff --git a/ManagedCode.FeatureChecker/FeatureChecker.cs b/ManagedCode.FeatureChecker/FeatureChecker.cs index 40c5b95..79569d3 100644 --- a/ManagedCode.FeatureChecker/FeatureChecker.cs +++ b/ManagedCode.FeatureChecker/FeatureChecker.cs @@ -1,110 +1,46 @@ -namespace ManagedCode.FeatureChecker; +using System.Collections.Immutable; + +namespace ManagedCode.FeatureChecker; public class FeatureChecker { - private readonly Dictionary _features; - - public FeatureChecker() - { - _features = new Dictionary(); - } - - public event EventHandler FeatureAdded; - public event EventHandler FeatureRemoved; - public event EventHandler FeatureStatusChanged; + private readonly ImmutableDictionary _features; + public int Count => _features.Count; - public bool TryAddFeature(string name, FeatureStatus status) + public FeatureChecker(FeatureHolder featureHolder) { - var feature = new Feature() - { - Name = name, - Status = status - }; - - return TryAddFeature(feature); + _features = featureHolder.Features; } - public bool TryAddFeature(Feature newFeature) - { - ThrowIfNull(newFeature, nameof(newFeature)); - ValidateModel(newFeature); - - return _features.TryAdd(newFeature.Name, newFeature); - } - - - public void RemoveFeature(string name) - { - ThrowIfNullOrEmpty(name, nameof(name)); - _features.Remove(name); - } - - public void RemoveAllFeatures() => _features.Clear(); - - public bool IsFeatureExists(string name) { - ThrowIfNullOrEmpty(name, nameof(name)); - - return _features.ContainsKey(name); + return ValidateFeatureName(name) + ? _features.ContainsKey(name) + : false; } public bool TryGetFeatureStatus(string name, out FeatureStatus status) { - var result = TryGetFeatureByName(name, out Feature? feature); - status = feature?.Status ?? default; + status = default; - return result; + return ValidateFeatureName(name) + ? _features.TryGetValue(name, out status) + : false; } - public bool TryGetFeatureByName(string name, out Feature? feature) + public List GetFeaturesByStatus(FeatureStatus status) { - ThrowIfNullOrEmpty(name, nameof(name)); - - return _features.TryGetValue(name, out feature); + return _features + .Where(x => x.Value == status) + .Select(x => x.Key) + .ToList(); } - public Feature[] GetExistFeatures() => _features.Values.ToArray(); - - public IEnumerable GetFeaturesByStatus(FeatureStatus status) + private bool ValidateFeatureName(string featureName) { - return _features.Values.Where(x => x.Status == status); + return !string.IsNullOrWhiteSpace(featureName); } - - public bool TryUpdateFeatureStatus(string name, FeatureStatus status) - { - var result = TryGetFeatureByName(name, out Feature? feature); - - if(result && feature != null) - { - feature.Status = FeatureStatus.Disabled; - } - - return result; - } - - - private static void ThrowIfNull(object obj, string paramName) - { - if(obj == null) - { - throw new ArgumentNullException(paramName, $"Parameter is null."); - } - } - - private static void ThrowIfNullOrEmpty(string arg, string argName) - { - if(string.IsNullOrWhiteSpace(arg)) - { - throw new ArgumentException($"Invalid parameter '{argName}': {arg}."); - } - } - - private static void ValidateModel(Feature feature) - { - //do some validation - } } diff --git a/ManagedCode.FeatureChecker/FeatureHolder.cs b/ManagedCode.FeatureChecker/FeatureHolder.cs new file mode 100644 index 0000000..a11ed60 --- /dev/null +++ b/ManagedCode.FeatureChecker/FeatureHolder.cs @@ -0,0 +1,61 @@ +using System.Collections.Immutable; +using System.Text.Json.Serialization; + +namespace ManagedCode.FeatureChecker; +public class FeatureHolder +{ + private Dictionary _features; + + [JsonInclude] + public ImmutableDictionary Features + { + get => _features.ToImmutableDictionary(); + private set => _features = new Dictionary(value); + } + + + public FeatureHolder() + { + _features = new Dictionary(); + } + + public bool TryAddFeature(string featureName, FeatureStatus status) + { + return ValidateFeatureName(featureName) + ? _features.TryAdd(featureName, status) + : false; + } + + public bool TryGetFeatureStatus(string featureName, out FeatureStatus status) + { + status = default; + + return ValidateFeatureName(featureName) + ? _features.TryGetValue(featureName, out status) + : false; + } + + public void RemoveFeature(string featureName) + { + if(ValidateFeatureName(featureName)) + { + _features.Remove(featureName); + } + } + + public void UpdateFeatureStatus(string featureName, FeatureStatus status) + { + if(!ValidateFeatureName(featureName)) + { + return; + } + + _features[featureName] = status; + } + + + private bool ValidateFeatureName(string featureName) + { + return !string.IsNullOrWhiteSpace(featureName); + } +} diff --git a/ManagedCode.FeatureChecker/FeatureStatus.cs b/ManagedCode.FeatureChecker/FeatureStatus.cs index 46edc00..00bfba5 100644 --- a/ManagedCode.FeatureChecker/FeatureStatus.cs +++ b/ManagedCode.FeatureChecker/FeatureStatus.cs @@ -3,6 +3,6 @@ public enum FeatureStatus { Disabled, - Eanabled, + Enabled, Debug, } diff --git a/ManagedCode.FeatureChecker/ManagedCode.FeatureChecker.csproj b/ManagedCode.FeatureChecker/ManagedCode.FeatureChecker.csproj index 5f33f2c..f2075a7 100644 --- a/ManagedCode.FeatureChecker/ManagedCode.FeatureChecker.csproj +++ b/ManagedCode.FeatureChecker/ManagedCode.FeatureChecker.csproj @@ -1,4 +1,4 @@ - + enable @@ -15,7 +15,9 @@ managedcode, Tests, Detector, TestsDetector, unit tests - + + + diff --git a/README.md b/README.md index 52f0cc7..cc922d6 100644 --- a/README.md +++ b/README.md @@ -1 +1,40 @@ # FeatureChecker + +## Usage + +```js + var holder = new FeatureHolder(); + + holder.TryAddFeature("feature 1", default); + holder.TryAddFeature("feature 2", FeatureStatus.Enabled); + holder.TryAddFeature("feature 3", FeatureStatus.Debug); + + holder.UpdateFeatureStatus("feature 3", FeatureStatus.Enabled); + + + var checker = new FeatureChecker(holder); + + if(checker.IsFeatureExists("feature_name")) + { + //do some things... + } + + + var enabledFeatures = checker.GetFeaturesByStatus(FeatureStatus.Enabled); + + foreach(var feat in enabledFeatures) + { + Console.WriteLine(feat); + //other code... + } + + + bool result = checker.TryGetFeatureStatus("myFeature", out FeatureStatus status); + + if(result) + { + Console.WriteLine(status); + //other code... + } + +``` \ No newline at end of file