⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 recordlist.cs

📁 一种.net实现ajax方法的类库
💻 CS
字号:
/** Copyright (c) 2006, All-In-One Creations, Ltd.*  All rights reserved.* * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:* *     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.*     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.*     * Neither the name of All-In-One Creations, Ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.* * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLEFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIALDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.**//** * Project: emergetk: stateful web framework for the masses * File name: RecordList.cs * Description: non-generic default implentation of IRecordList.  Should cover most cases, but you will want to * write a custom implementation of IRecordList and IRecordListT for when you must be sensitive to storage allocations, * are pulling data from forward-only cursors, etc. *    * Author: Ben Joldersma *    */using System;using System.Collections;using System.Collections.Generic;using System.Web;using System.Reflection;using System.Data;namespace EmergeTk.Model{    public delegate void RecordListHandler(IRecordList list);	/// <summary>	/// Summary description for RecordList.	/// </summary>    public class RecordList : IRecordList, IComparable	{		private List<AbstractRecord> items;        private Type recordType;        public Type RecordType        {            get { return recordType; }            set { recordType = value; }        }			public RecordList()		{            items = new List<AbstractRecord>();		}		#region IList Members		public bool IsReadOnly		{			get			{				// TODO:  Add RecordList.IsReadOnly getter implementation				return false;			}		}        public AbstractRecord this[int index]		{			get			{				return items[index] as AbstractRecord;			}			set			{				items[index] = value;			}		}        private bool live = false;        private bool listening = false;        RecordHandler onGlobalChanged, onGlobalDeleted, onGlobalCreated;        public bool Live        {            get { return live; }            set            {                live = value;                if (value && !listening)                {                    if (onGlobalChanged == null)                    {                        onGlobalChanged = new RecordHandler(OnGlobalRecordChanged);                        onGlobalDeleted = new RecordHandler(OnGlobalRecordDeleted);                        onGlobalCreated = new RecordHandler(OnGlobalRecordCreated);                    }                    AbstractRecord.RegisterChangedListener(typeof(AbstractRecord),onGlobalChanged );                    AbstractRecord.RegisterDeletedListener(typeof(AbstractRecord), onGlobalDeleted);                    AbstractRecord.RegisterNewListener(typeof(AbstractRecord), onGlobalCreated);                }                else if (! value && listening )                {                    AbstractRecord.UnregisterChangedListener(typeof(AbstractRecord), onGlobalChanged);                    AbstractRecord.UnregisterDeletedListener(typeof(AbstractRecord), onGlobalDeleted);                    AbstractRecord.UnregisterNewListener(typeof(AbstractRecord), onGlobalCreated);                }            }        }        private void OnGlobalRecordChanged(AbstractRecord r)        {            if (filters != null && filters.Count > 0)            {                bool isMember = true;                foreach (FilterInfo fi in filters)                {                    if (!FilterInfo.Filter(fi.Operation, fi.Value, r[fi.ColumnName]))                    {                        isMember = false;                        break;                    }                }                if (isMember && !Contains(r as AbstractRecord))                    Add(r as AbstractRecord);                else if (!isMember && Contains(r as AbstractRecord))                    Remove(r as AbstractRecord);            }        }        private void OnGlobalRecordCreated(AbstractRecord r)        {            Add(r as AbstractRecord);        }        private void OnGlobalRecordDeleted(AbstractRecord r)        {            Remove(r as AbstractRecord);        }		public void RemoveAt(int index)		{            AbstractRecord r = items[index] as AbstractRecord;			items.RemoveAt(index);		}        public void Insert(int index, AbstractRecord value)		{			items.Insert( index, value );		}        private List<SortInfo> sorts;        public List<SortInfo> Sorts        {            get { if( sorts == null ) sorts = new List<SortInfo>(); return sorts; }            set { sorts = value; }        }        private List<FilterInfo> filters;        public List<FilterInfo> Filters        {            get { if (filters == null) filters = new List<FilterInfo>(); return filters; }            set { filters = value; }        }        public void Remove(AbstractRecord value)		{			items.Remove( value );            if (onRecordRemoved != null)                onRecordRemoved(value);		}        public void Delete(AbstractRecord value)        {            Remove(value);            value.Delete();        }		public bool Contains(AbstractRecord value)		{			return items.Contains( value );		}		public void Clear()		{            items.Clear();		}        public int IndexOf(AbstractRecord value)		{			return items.IndexOf( value );		}        public void Add(AbstractRecord value)		{			items.Add( value );            value.OnDelete += new RecordHandler(value_OnDelete);            if( onRecordChanged != null)                value.OnChange += onRecordChanged;            if (OnRecordAdded != null)                OnRecordAdded(value);		}        void value_OnDelete(AbstractRecord r)        {            Remove(r);        }        public AbstractRecord NewRow<T>() where T : AbstractRecord, new()        {            AbstractRecord n = new T();            Add(n);            return n;        }		public int Count		{			get			{				return items.Count;			}		}		#endregion        public void Sort()        {            if (sorts != null)            {                items.Sort( new RecordComparer<AbstractRecord>(sorts) );            }        }        public void AddFilter(FilterInfo filterInfo)        {            if (filters == null) filters = new List<FilterInfo>();            filters.Add(filterInfo);        }        public void Filter()        {            if (filters != null)            {                foreach (AbstractRecord r in items)                {                    foreach (FilterInfo fi in filters)                    {                        if (!FilterInfo.Filter(fi.Operation, r[fi.ColumnName], fi.Value))                            items.Remove(r);                    }                }            }        }                public string[] ToStringArray()        {            List<string> strings = new List<string>(this.Count);            foreach (AbstractRecord r in this)            {                strings.Add(r.ToString());            }            return strings.ToArray();        }        public string[] ToStringArray(string property)        {            List<string> strings = new List<string>(this.Count);            foreach (AbstractRecord r in this)            {                strings.Add(r[property].ToString());            }            return strings.ToArray();        }        public string[] ToIdArray()        {            List<string> ids = new List<string>(this.Count);            foreach (AbstractRecord r in this)            {                ids.Add(r.Id.ToString());            }            return ids.ToArray();        }		public void Save()		{            items.ForEach(new Action<AbstractRecord>(delegate(AbstractRecord r) { r.Save(); }));		}        public void ForEach(Action<AbstractRecord> action)        {            items.ForEach(action);        }		public void Delete()		{            items.ForEach(new Action<AbstractRecord>(delegate(AbstractRecord r) { r.Delete(); }));            Clear();		}        public void DeleteAt(int index)        {            this[index].Delete();            RemoveAt(index);        }        public string toJSON(JSON.Type transportType, List<DataGridColumn> columns, int pageIndex, int pageSize )        {            List<string> records = new List<string>();            for( int i = pageIndex * pageSize; (i < pageIndex * pageSize + pageSize )&& i < this.Count; i++ )            {                records.Add(this[i].toJSON(transportType, columns ));            }            return JSON.ArrayToJSON( records );        }        //Events        private event RecordHandler onRecordChanged;        public event RecordHandler OnRecordChanged        {            add {                 onRecordChanged += value;                foreach (AbstractRecord r in this)                    r.OnChange += value;            }            remove {                onRecordChanged -= value;                foreach (AbstractRecord r in this)                    r.OnChange -= value;            }        }        private event RecordHandler onRecordRemoved;        public event RecordHandler OnRecordRemoved        {            add            {                onRecordRemoved += value;                foreach (AbstractRecord r in this)                    r.OnDelete += value;            }            remove            {                onRecordRemoved -= value;                foreach (AbstractRecord r in this)                    r.OnDelete -= value;            }        }        public event RecordHandler OnRecordAdded;        public IEnumerator<AbstractRecord> GetEnumerator()        {            return items.GetEnumerator();        }        #region IComparable Members        public int CompareTo(object obj)        {            IRecordList other = obj as IRecordList;            if (other != null)            {                int myWeight = 0, otherWeight = 0;                foreach (AbstractRecord r in this)                {                    myWeight += r.ComputeRecordWeight();                }                foreach (AbstractRecord r in other)                {                    otherWeight += r.ComputeRecordWeight();                }                return myWeight - otherWeight;            }            return 0;        }        #endregion    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -