Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. [Serializable]
  2. [JsonObject]
  3. public class TrackableCollectionCollection<T> : IList<T> where T : ITrackableEntity
  4. {
  5. [JsonIgnore]
  6. IList<T> _list = new List<T>();
  7.  
  8. [JsonProperty]
  9. public IList<T> List
  10. {
  11. get { return _list; }
  12. set
  13. {
  14. _list = value;
  15.  
  16. foreach(var item in _list)
  17. item.PropertyChanged += item_PropertyChanged;
  18. }
  19. }
  20.  
  21. [DataMember]
  22. public IList<T> NewItems
  23. {
  24. get { return _newItems; }
  25. }
  26. IList<T> _newItems = new List<T>();
  27.  
  28. [DataMember]
  29. public IList<T> ModifiedItems
  30. {
  31. get { return _modifiedChildren; }
  32. }
  33. IList<T> _modifiedChildren = new List<T>();
  34.  
  35. [DataMember]
  36. public IList<T> DeletedItems
  37. {
  38. get { return _deletedItems; }
  39. }
  40. IList<T> _deletedItems = new List<T>();
  41.  
  42. #region Implementation of IEnumerable
  43.  
  44. public IEnumerator<T> GetEnumerator()
  45. {
  46. return _list.GetEnumerator();
  47. }
  48.  
  49. IEnumerator IEnumerable.GetEnumerator()
  50. {
  51. return GetEnumerator();
  52. }
  53.  
  54. #endregion
  55.  
  56. #region Implementation of ICollection<T>
  57.  
  58. public void Add(T item)
  59. {
  60. if (item.Id.Equals(default(Guid)))
  61. _newItems.Add(item);
  62. else
  63. {
  64. // I thought about doing this but that would screw the EF object generation.
  65. // throw new NotSupportedException("");
  66. }
  67.  
  68. item.PropertyChanged += item_PropertyChanged;
  69.  
  70. _list.Add(item);
  71. }
  72.  
  73.  
  74. public void Clear()
  75. {
  76. NewItems.Clear();
  77. ModifiedItems.Clear();
  78.  
  79. foreach(var item in _list)
  80. {
  81. item.PropertyChanged -= item_PropertyChanged;
  82. DeletedItems.Add(item);
  83. }
  84.  
  85. _list.Clear();
  86. }
  87.  
  88. public bool Contains(T item)
  89. {
  90. return _list.Contains(item);
  91. }
  92.  
  93. public void CopyTo(T[] array, int arrayIndex)
  94. {
  95. _list.CopyTo(array, arrayIndex);
  96. }
  97.  
  98. public bool Remove(T item)
  99. {
  100. if (NewItems.Contains(item))
  101. NewItems.Remove(item);
  102.  
  103. if (ModifiedItems.Contains(item))
  104. ModifiedItems.Remove(item);
  105.  
  106. if (!DeletedItems.Contains(item))
  107. DeletedItems.Add(item);
  108.  
  109. return _list.Remove(item);
  110. }
  111.  
  112. public int Count
  113. {
  114. get { return _list.Count; }
  115. }
  116.  
  117. public bool IsReadOnly
  118. {
  119. get { return _list.IsReadOnly; }
  120. }
  121.  
  122. #endregion
  123.  
  124. #region Implementation of IList<T>
  125.  
  126. public int IndexOf(T item)
  127. {
  128. return _list.IndexOf(item);
  129. }
  130.  
  131. public void Insert(int index, T item)
  132. {
  133. if (item.Id.Equals(default(Guid)))
  134. _newItems.Add(item);
  135. else
  136. {
  137. // I thought about doing this but that would screw the EF object generation.
  138. // throw new NotSupportedException("");
  139. }
  140.  
  141. item.PropertyChanged += item_PropertyChanged;
  142.  
  143. _list.Insert(index, item);
  144. }
  145.  
  146. public void RemoveAt(int index)
  147. {
  148. var item = this[index];
  149.  
  150. if (NewItems.Contains(item))
  151. NewItems.Remove(item);
  152.  
  153. if (ModifiedItems.Contains(item))
  154. ModifiedItems.Remove(item);
  155.  
  156. if (!DeletedItems.Contains(item))
  157. DeletedItems.Add(item);
  158.  
  159. _list.RemoveAt(index);
  160. }
  161.  
  162. public T this[int index]
  163. {
  164. get { return _list[index]; }
  165. set { _list[index] = value; }
  166. }
  167. #endregion
  168. void item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  169. {
  170. if (((T)sender).Id.Equals(default(Guid)))
  171. return; // The Item is already in the newItems collection
  172.  
  173. if (ModifiedItems.Contains((T)sender))
  174. return;
  175.  
  176. ModifiedItems.Add((T)sender);
  177.  
  178. }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement