Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions ManagedCode.FeatureChecker/Feature.cs

This file was deleted.

106 changes: 21 additions & 85 deletions ManagedCode.FeatureChecker/FeatureChecker.cs
Original file line number Diff line number Diff line change
@@ -1,110 +1,46 @@
namespace ManagedCode.FeatureChecker;
using System.Collections.Immutable;

namespace ManagedCode.FeatureChecker;

public class FeatureChecker
{
private readonly Dictionary<string, Feature> _features;

public FeatureChecker()
{
_features = new Dictionary<string, Feature>();
}

public event EventHandler FeatureAdded;
public event EventHandler FeatureRemoved;
public event EventHandler FeatureStatusChanged;
private readonly ImmutableDictionary<string, FeatureStatus> _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<string> 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<Feature> 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
}
}
61 changes: 61 additions & 0 deletions ManagedCode.FeatureChecker/FeatureHolder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Collections.Immutable;
using System.Text.Json.Serialization;

namespace ManagedCode.FeatureChecker;
public class FeatureHolder
{
private Dictionary<string, FeatureStatus> _features;

[JsonInclude]
public ImmutableDictionary<string, FeatureStatus> Features
{
get => _features.ToImmutableDictionary();
private set => _features = new Dictionary<string, FeatureStatus>(value);
}


public FeatureHolder()
{
_features = new Dictionary<string, FeatureStatus>();
}

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);
}
}
2 changes: 1 addition & 1 deletion ManagedCode.FeatureChecker/FeatureStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public enum FeatureStatus
{
Disabled,
Eanabled,
Enabled,
Debug,
}
6 changes: 4 additions & 2 deletions ManagedCode.FeatureChecker/ManagedCode.FeatureChecker.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -15,7 +15,9 @@
<PackageTags>managedcode, Tests, Detector, TestsDetector, unit tests</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageReference Include="System.Collections.Immutable" Version="7.0.0" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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...
}

```