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: 6 additions & 0 deletions ManagedCode.FeatureChecker/Feature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ManagedCode.FeatureChecker;
public class Feature
{
public string Name { get; set; } = null!;
public FeatureStatus Status { get; set; }
}
110 changes: 110 additions & 0 deletions ManagedCode.FeatureChecker/FeatureChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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;


public bool TryAddFeature(string name, FeatureStatus status)
{
var feature = new Feature()
{
Name = name,
Status = status
};

return TryAddFeature(feature);
}

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);
}

public bool TryGetFeatureStatus(string name, out FeatureStatus status)
{
var result = TryGetFeatureByName(name, out Feature? feature);
status = feature?.Status ?? default;

return result;
}

public bool TryGetFeatureByName(string name, out Feature? feature)
{
ThrowIfNullOrEmpty(name, nameof(name));

return _features.TryGetValue(name, out feature);
}


public Feature[] GetExistFeatures() => _features.Values.ToArray();

public IEnumerable<Feature> GetFeaturesByStatus(FeatureStatus status)
{
return _features.Values.Where(x => x.Status == status);
}


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
}
}
8 changes: 8 additions & 0 deletions ManagedCode.FeatureChecker/FeatureStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ManagedCode.FeatureChecker;

public enum FeatureStatus
{
Disabled,
Eanabled,
Debug,
}