实体和实体的集合

这几天在研究实体和实体的集合的写法,基本上完成了数据绑定,但是还是没有完全完成

  1using System;
  2using System.Collections;
  3using System.ComponentModel;
  4
  5namespace Entity
  6{
  7    /// <summary>
  8    /// 类OrderInfo的集合
  9    /// </summary>

 10    public class OrderInfoCollection : CollectionBase,IBindingList
 11    {
 12        #region CollectionBase实现
 13
 14        public OrderInfoCollection() 
 15        {
 16        }

 17
 18        public OrderInfoCollection(OrderInfo[] value)
 19        {
 20            this.AddRange(value);
 21        }

 22                
 23        public OrderInfo this[int index] 
 24        {
 25            get{return ((OrderInfo)(this.List[index]));}
 26            set{List[index] = value;}
 27        }

 28
 29        public int Add(OrderInfo value) 
 30        {
 31            return this.List.Add(value);
 32        }

 33
 34        public OrderInfo AddNew() 
 35        {
 36            return (OrderInfo)((IBindingList)this).AddNew();
 37        }

 38
 39        object IBindingList.AddNew()
 40        {
 41            OrderInfo c = new OrderInfo();
 42            List.Add(c);
 43            return c;
 44        }

 45
 46        public void AddRange(OrderInfo[] value) 
 47        {
 48            for (int i = 0;    (i < value.Length); i = (i + 1)) 
 49            {
 50                this.Add(value[i]);
 51            }

 52        }

 53
 54        public void AddRange(OrderInfoCollection value) 
 55        {
 56            for (int i = 0;    (i < value.Count); i = (i +    1))    
 57            {
 58                this.Add((OrderInfo)value.List[i]);
 59            }

 60        }

 61
 62        public bool Contains(OrderInfo value) 
 63        {
 64            return this.List.Contains(value);
 65        }

 66
 67        public void CopyTo(OrderInfo[] array, int index) 
 68        {
 69            this.List.CopyTo(array, index);
 70        }

 71
 72        public int IndexOf(OrderInfo value) 
 73        {
 74            return this.List.IndexOf(value);
 75        }

 76
 77        public void Insert(int index, OrderInfo value)    
 78        {
 79            List.Insert(index, value);
 80        }

 81
 82        public void Remove(OrderInfo value) 
 83        {
 84            List.Remove(value);
 85        }

 86
 87        public new OrderInfoCollectionEnumerator GetEnumerator()    
 88        {
 89            return new OrderInfoCollectionEnumerator(this);
 90        }

 91
 92        #endregion

 93
 94
 95        #region OrderInfoCollectionEnumerator 实现
 96
 97        public class OrderInfoCollectionEnumerator : IEnumerator    
 98        {
 99            private    IEnumerator _enumerator;
100            private    IEnumerable _temp;
101            
102            public OrderInfoCollectionEnumerator(OrderInfoCollection mappings)
103            {
104                _temp =    ((IEnumerable)(mappings));
105                _enumerator = _temp.GetEnumerator();
106            }

107            
108            public OrderInfo Current
109            {
110                get {return ((OrderInfo)(_enumerator.Current));}
111            }

112            
113            object IEnumerator.Current
114            {
115                get {return _enumerator.Current;}
116            }

117            
118            public bool MoveNext()
119            {
120                return _enumerator.MoveNext();
121            }

122            
123            bool IEnumerator.MoveNext()
124            {
125                return _enumerator.MoveNext();
126            }

127            
128            public void Reset()
129            {
130                _enumerator.Reset();
131            }

132            
133            void IEnumerator.Reset() 
134            {
135                _enumerator.Reset();
136            }

137        }

138        #endregion
 
139
140
141        #region IBindingList 成员
142
143        private ListChangedEventArgs resetEvent = new ListChangedEventArgs(ListChangedType.Reset, -1);
144        private ListChangedEventHandler onListChanged;
145
146        public bool AllowEdit 
147        
148            get {return true;}
149        }

150
151        public bool AllowNew 
152        
153            get {return true;}
154        }

155
156        public bool AllowRemove 
157        
158            get {return true;}
159        }

160
161        public bool SupportsChangeNotification 
162        
163            get {return true;}
164        }

165
166        public bool SupportsSearching 
167        
168            get {return false;}
169        }

170
171        public bool SupportsSorting
172        
173            get {return false;}
174        }

175
176        public void AddIndex(PropertyDescriptor property)
177        {
178            throw new NotSupportedException();
179        }

180        public void ApplySort(PropertyDescriptor property, System.ComponentModel.ListSortDirection direction)
181        {
182            throw new NotSupportedException();
183        }

184        public PropertyDescriptor SortProperty
185        {
186            get{throw new NotSupportedException();
187                //return null;
188            }

189        }

190        public int Find(PropertyDescriptor property, object key)
191        {
192            throw new NotSupportedException(); 
193            //return 0;
194        }

195        public void RemoveSort()
196        {
197            throw new NotSupportedException(); 
198        }

199        public void RemoveIndex(PropertyDescriptor property)
200        {
201            throw new NotSupportedException(); 
202        }

203        public bool IsSorted
204        {
205            get throw new NotSupportedException();
206            //return false;
207            }

208        }

209        public System.ComponentModel.ListSortDirection SortDirection
210        {
211            get{throw new NotSupportedException();
212                //return new System.ComponentModel.ListSortDirection ();
213            }

214        }

215        public event ListChangedEventHandler ListChanged 
216        {
217            add{onListChanged += value;}
218            remove{onListChanged -= value;}
219        }

220
221        protected virtual void OnListChanged(ListChangedEventArgs ev) 
222        {
223            if (onListChanged != null
224            {
225                onListChanged(this, ev);
226            }

227        }

228        protected override void OnClearComplete() 
229        {
230            OnListChanged(resetEvent);
231        }

232        private void RemoveChild(Object source, OrderInfo.OrderInfoEventArgs e)
233        {
234            List.Remove(source);
235        }

236        protected override void OnInsertComplete(int index, object value) 
237        {
238            ((OrderInfo)(value)).RemoveMe += new OrderInfo.OrderInfoEventHandler(RemoveChild); 
239            OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
240        }

241        protected override void OnRemoveComplete(int index, object value) 
242        {
243            OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index));
244        }

245        protected override void OnSetComplete(int index, object oldValue, object newValue) 
246        {
247            if (oldValue != newValue) 
248            {
249                OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
250            }

251        }

252
253        #endregion

254    }

255}

上面是实体的集合,下面是实体的写法
  1using System;
  2using System.Data;
  3using System.Collections;
  4using System.ComponentModel;
  5
  6namespace Entity
  7{
  8    /// <summary>
  9    /// Order的描述
 10    /// </summary>

 11    public class OrderInfo : IEditableObject,IDataErrorInfo
 12    {
 13
 14        #region 实体 结构体
 15        /// <summary>
 16        /// 类OrderInfo的实体结构体
 17        /// </summary>

 18        struct OrderInfoData
 19        {
 20            internal string _id;
 21            internal string _name;
 22        }

 23
 24        #endregion

 25
 26
 27        #region 私有变量
 28
 29        private OrderInfoData custData;    //类实体的值
 30        private OrderInfoData backupData;    //类实体的备份值(用于CancelEdit的时候的恢复)
 31        private bool mEditing = false;            //是否处于编辑状态
 32        private bool mIsNew = true;                //是否是新建状态
 33
 34        #endregion

 35
 36
 37        #region 构造函数
 38
 39        /// <summary>
 40        /// 默认构造函数
 41        /// </summary>

 42        public OrderInfo():base()
 43        {
 44            this.custData = new OrderInfoData();
 45            this.custData._id = "";
 46            this.custData._name = "";
 47        }

 48
 49
 50        /// <summary>
 51        /// 构造函数,所有公开属性赋值
 52        /// </summary>
 53        /// <param name="m_id">属性ID的描述</param>
 54        /// <param name="m_name">属性Name的描述</param>

 55        public OrderInfo(string m_id,string m_name)
 56        {
 57            this.custData = new OrderInfoData();
 58            this.custData._id = m_id;
 59            this.custData._name = m_name;
 60        }

 61
 62        #endregion

 63
 64
 65        #region 实体属性
 66
 67        /// <summary>
 68        /// 属性ID的描述
 69        /// </summary>

 70        public string ID
 71        {
 72            get{return this.custData._id;}
 73            set{this.custData._id = value;}
 74        }

 75
 76        /// <summary>
 77        /// 属性Name的描述
 78        /// </summary>

 79        public string Name
 80        {
 81            get{return this.custData._name;}
 82            set{this.custData._name = value;}
 83        }

 84
 85
 86        #endregion

 87
 88
 89        #region IEditableObject 成员
 90
 91        public void EndEdit()
 92        {
 93            if (mEditing) 
 94            {
 95                mEditing = false;
 96                mIsNew = false;
 97            }

 98        }

 99
100        internal class OrderInfoEventArgs : EventArgs
101        {
102            // 定义事件成员,用于提供有关事件的信息
103        }

104        internal delegate void OrderInfoEventHandler(Object source, OrderInfoEventArgs e);
105        internal event OrderInfoEventHandler RemoveMe;
106        public void CancelEdit()
107        {
108            if (mEditing) 
109            {
110                mEditing = false;
111                this.custData = backupData;
112                if(mIsNew)
113                {
114                    mIsNew = false;
115                    RemoveMe(thisnew OrderInfoEventArgs());
116                }

117            }

118        }

119
120        public void BeginEdit()
121        {
122            if (!mEditing) 
123            {
124                mEditing = true;
125                this.backupData = custData;
126            }

127        }

128
129        #endregion

130
131
132        #region IDataErrorInfo 成员
133
134        public string Error
135        
136            get{return "";}
137        }
 
138
139        public string this[string strErrorName]  
140        
141            get{return "";}
142        }
 
143
144        #endregion

145    }

146}
from:http://zellzhang.cnblogs.com/archive/2005/07/11/190538.html
posted @ 2006-01-22 02:24  torome  阅读(396)  评论(0编辑  收藏  举报