using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; namespace BookAStar.Data { public class LocalEntity : ObservableCollection, ILocalEntity where K : IEquatable { public V CurrentItem { get; protected set; } public Func GetKey { get; set; } public LocalEntity(Func getKey) : base() { if (getKey == null) throw new InvalidOperationException(); GetKey = getKey; IList l = this; } public virtual void Merge(V item) { var key = GetKey(item); if (this.Any(x => GetKey(x).Equals(key))) { Remove(LocalGet(key)); } Add(item); CurrentItem = item; } public V LocalGet(K key) { if (!this.Any(x => GetKey(x).Equals(key))) return default(V); CurrentItem = this.Single(x => GetKey(x).Equals(key)); return CurrentItem; } public void Load() { this.Populate(); } public void Load(string subKey) { this.Populate(subKey); } } }