Skip to content
Open
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
22 changes: 22 additions & 0 deletions NFeatureGate/Events/NFeatureChanged.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NFeatureGate.Events
{

public class NFeatureChangedEventArgs : EventArgs
{
public string Name { get; private set; }

public NFeatureChangedEventArgs(NFeature feature)
{
Name = feature.Name;
}
}

public delegate void NFeatureChangedEventHandler(NFeature sender, NFeatureChangedEventArgs args);

}
27 changes: 21 additions & 6 deletions NFeatureGate/NBranchFeatureStateCollection.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Collections.Generic;
using NFeatureGate.Events;
using System;
using System.Collections.Generic;
using System.Linq;

namespace NFeatureGate
{
public class NBranchFeatureStateCollection : IEnumerable<NBranchFeatureState>
{
private readonly List<NBranchFeatureState> _states = new List<NBranchFeatureState>();
private readonly IDictionary<string, NBranchFeatureState> _states = new Dictionary<string, NBranchFeatureState>(StringComparer.OrdinalIgnoreCase);

public NBranchFeatureStateCollection()
{
Expand All @@ -14,22 +16,35 @@ public NBranchFeatureStateCollection()

public NBranchFeatureStateCollection(IEnumerable<NBranchFeatureState> branchFeatureStates)
{
_states = branchFeatureStates.ToList();
branchFeatureStates.ToList().ForEach(s =>
{
s.Feature.NameChanged += UpdateFeatureName;
});
}

public NBranchFeatureState this[string name]
{
get { return _states.FirstOrDefault(n => n.Feature.Name.Trim().ToLower().Equals(name.Trim().ToLower())); }
get { return _states[name.Trim()]; }
}

public IEnumerator<NBranchFeatureState> GetEnumerator()
{
return _states.GetEnumerator();
return _states.Values.GetEnumerator();
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _states.GetEnumerator();
return _states.Values.GetEnumerator();
}

private void UpdateFeatureName(NFeature sender, NFeatureChangedEventArgs args)
{
var state = _states[args.Name];
if (state == null)
throw new InvalidOperationException("State Collection received event for non-existant feature");

_states.Remove(args.Name);
_states.Add(args.Name, state);
}
}
}
25 changes: 24 additions & 1 deletion NFeatureGate/NFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,34 @@
using System.Text;
using System.Threading.Tasks;
using NFeatureGate.Contracts;
using NFeatureGate.Events;

namespace NFeatureGate
{
public class NFeature
{
public string Name { get; set; }
public event NFeatureChangedEventHandler NameChanged;

private string _name;

public string Name
{
get
{
return _name;
}

set
{
var args = new NFeatureChangedEventArgs(this);
_name = value.Trim();
OnNameChanged(args);
}
}

protected virtual void OnNameChanged(NFeatureChangedEventArgs args)
{
if (NameChanged != null) NameChanged(this, args);
}
}
}