Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Serializable]
- [JsonObject]
- public class TrackableCollectionCollection<T> : IList<T> where T : ITrackableEntity
- {
- [JsonIgnore]
- IList<T> _list = new List<T>();
- [JsonProperty]
- public IList<T> List
- {
- get { return _list; }
- set
- {
- _list = value;
- foreach(var item in _list)
- item.PropertyChanged += item_PropertyChanged;
- }
- }
- [DataMember]
- public IList<T> NewItems
- {
- get { return _newItems; }
- }
- IList<T> _newItems = new List<T>();
- [DataMember]
- public IList<T> ModifiedItems
- {
- get { return _modifiedChildren; }
- }
- IList<T> _modifiedChildren = new List<T>();
- [DataMember]
- public IList<T> DeletedItems
- {
- get { return _deletedItems; }
- }
- IList<T> _deletedItems = new List<T>();
- #region Implementation of IEnumerable
- public IEnumerator<T> GetEnumerator()
- {
- return _list.GetEnumerator();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- #endregion
- #region Implementation of ICollection<T>
- public void Add(T item)
- {
- if (item.Id.Equals(default(Guid)))
- _newItems.Add(item);
- else
- {
- // I thought about doing this but that would screw the EF object generation.
- // throw new NotSupportedException("");
- }
- item.PropertyChanged += item_PropertyChanged;
- _list.Add(item);
- }
- public void Clear()
- {
- NewItems.Clear();
- ModifiedItems.Clear();
- foreach(var item in _list)
- {
- item.PropertyChanged -= item_PropertyChanged;
- DeletedItems.Add(item);
- }
- _list.Clear();
- }
- public bool Contains(T item)
- {
- return _list.Contains(item);
- }
- public void CopyTo(T[] array, int arrayIndex)
- {
- _list.CopyTo(array, arrayIndex);
- }
- public bool Remove(T item)
- {
- if (NewItems.Contains(item))
- NewItems.Remove(item);
- if (ModifiedItems.Contains(item))
- ModifiedItems.Remove(item);
- if (!DeletedItems.Contains(item))
- DeletedItems.Add(item);
- return _list.Remove(item);
- }
- public int Count
- {
- get { return _list.Count; }
- }
- public bool IsReadOnly
- {
- get { return _list.IsReadOnly; }
- }
- #endregion
- #region Implementation of IList<T>
- public int IndexOf(T item)
- {
- return _list.IndexOf(item);
- }
- public void Insert(int index, T item)
- {
- if (item.Id.Equals(default(Guid)))
- _newItems.Add(item);
- else
- {
- // I thought about doing this but that would screw the EF object generation.
- // throw new NotSupportedException("");
- }
- item.PropertyChanged += item_PropertyChanged;
- _list.Insert(index, item);
- }
- public void RemoveAt(int index)
- {
- var item = this[index];
- if (NewItems.Contains(item))
- NewItems.Remove(item);
- if (ModifiedItems.Contains(item))
- ModifiedItems.Remove(item);
- if (!DeletedItems.Contains(item))
- DeletedItems.Add(item);
- _list.RemoveAt(index);
- }
- public T this[int index]
- {
- get { return _list[index]; }
- set { _list[index] = value; }
- }
- #endregion
- void item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
- {
- if (((T)sender).Id.Equals(default(Guid)))
- return; // The Item is already in the newItems collection
- if (ModifiedItems.Contains((T)sender))
- return;
- ModifiedItems.Add((T)sender);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement