Go to the documentation of this file.00001 using System.Collections.Generic;
00002 using System.Configuration;
00003 using System.Xml;
00004 using System.Linq;
00005 using System.Collections;
00006
00007 namespace N2.Configuration
00008 {
00014 public abstract class LazyRemovableCollection<T> : ConfigurationElementCollection, IEnumerable<T>, IEnumerable
00015 where T : ConfigurationElement, IIdentifiable, new()
00016 {
00017 List<T> removedElements = new List<T>();
00018 List<T> defaults = new List<T>();
00019 bool isCleared;
00020
00022 public IEnumerable<T> RemovedElements
00023 {
00024 get { return removedElements.AsReadOnly(); }
00025 set { removedElements = new List<T>(value ?? new T[0]); }
00026 }
00027
00029 public IEnumerable<T> Defaults
00030 {
00031 get { return defaults.AsReadOnly(); }
00032 set { defaults = new List<T>(value ?? new T[0]); }
00033 }
00034
00036 public IEnumerable<T> AddedElements
00037 {
00038 get
00039 {
00040 object[] removedKeys = RemovedElements.Select(e => e.ElementKey).ToArray();
00041 foreach (object key in base.BaseGetAllKeys())
00042 {
00043 if (!removedKeys.Contains(key))
00044 yield return base.BaseGet(key) as T;
00045 }
00046 }
00047 }
00048
00050 public IEnumerable<T> AllElements
00051 {
00052 get
00053 {
00054 object[] removedKeys = RemovedElements.Select(e => e.ElementKey).ToArray();
00055 foreach (T element in Defaults)
00056 {
00057 var key = ((IIdentifiable)element).ElementKey;
00058 if (!removedKeys.Contains(key))
00059 yield return element;
00060 }
00061 foreach (T element in AddedElements)
00062 {
00063 yield return element;
00064 }
00065 }
00066 set
00067 {
00068 BaseClear();
00069 defaults.Clear();
00070 if(value == null) return;
00071
00072 foreach (T element in value)
00073 BaseAdd(element);
00074 }
00075 }
00076
00077 public bool IsCleared
00078 {
00079 get { return isCleared; }
00080 }
00081
00084 public void AddDefault(T element)
00085 {
00086 defaults.Add(element);
00087 }
00088
00091 public void Add(T element)
00092 {
00093 BaseAdd(element);
00094 }
00095
00098 public void Remove(T element)
00099 {
00100 removedElements.Add(element);
00101 }
00102
00104 public void Clear()
00105 {
00106 isCleared = true;
00107 defaults.Clear();
00108 BaseClear();
00109 }
00110
00111 protected override ConfigurationElement CreateNewElement()
00112 {
00113 return new T();
00114 }
00115
00116 protected override object GetElementKey(ConfigurationElement element)
00117 {
00118 return ((IIdentifiable)element).ElementKey;
00119 }
00120
00121 protected override bool OnDeserializeUnrecognizedElement(string elementName, XmlReader reader)
00122 {
00123 if (elementName == "remove")
00124 {
00125 T element = new T();
00126 element.ElementKey = reader.GetAttribute("name");
00127
00128 OnDeserializeRemoveElement(element, reader);
00129
00130 Remove(element);
00131 return true;
00132 }
00133 if(elementName == "clear")
00134 {
00135 Clear();
00136 return true;
00137 }
00138 return base.OnDeserializeUnrecognizedElement(elementName, reader);
00139 }
00140
00141 protected virtual void OnDeserializeRemoveElement(T element, XmlReader reader)
00142 {
00143 }
00144
00145 public new int Count
00146 {
00147 get { return AllElements.Count(); }
00148 }
00149
00150 #region IEnumerable<T> Members
00151
00152 public new IEnumerator<T> GetEnumerator()
00153 {
00154 return AllElements.GetEnumerator();
00155 }
00156
00157 #endregion
00158
00159 #region IEnumerable Members
00160
00161 IEnumerator IEnumerable.GetEnumerator()
00162 {
00163 return AllElements.GetEnumerator();
00164 }
00165
00166 #endregion
00167 }
00168 }