diff --git a/NFeatureGate/Events/NFeatureChanged.cs b/NFeatureGate/Events/NFeatureChanged.cs new file mode 100755 index 0000000..a053dab --- /dev/null +++ b/NFeatureGate/Events/NFeatureChanged.cs @@ -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); + +} diff --git a/NFeatureGate/NBranchFeatureStateCollection.cs b/NFeatureGate/NBranchFeatureStateCollection.cs index fdc81d1..ab90b27 100644 --- a/NFeatureGate/NBranchFeatureStateCollection.cs +++ b/NFeatureGate/NBranchFeatureStateCollection.cs @@ -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 { - private readonly List _states = new List(); + private readonly IDictionary _states = new Dictionary(StringComparer.OrdinalIgnoreCase); public NBranchFeatureStateCollection() { @@ -14,22 +16,35 @@ public NBranchFeatureStateCollection() public NBranchFeatureStateCollection(IEnumerable 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 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); } } } diff --git a/NFeatureGate/NFeature.cs b/NFeatureGate/NFeature.cs index 3d2fc28..8f1b8a0 100644 --- a/NFeatureGate/NFeature.cs +++ b/NFeatureGate/NFeature.cs @@ -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); + } } }