部分代码:
数据实体对象:
using System;
using VUser.ECommerce.DataProxy;
namespace VUser.ECommerce.Core
{
[Serializable]
public class BaseObject : IComparable
{
#region 域成员
private int m_ID;
private string m_Title;
private string m_Remark;
private IDataProxy m_DataProxy;
private bool m_IsModify = false;
private bool m_IsDirty = false;
private string m_SortFieldName;
#endregion
#region 属性
public virtual int ID
{
set
{
m_ID = value;
}
get
{
return m_ID;
}
}
/// <summary>
/// 名称
/// </summary>
public virtual string Title
{
set
{
m_Title = value;
}
get
{
return m_Title;
}
}
/// <summary>
/// 注释
/// </summary>
public string Remark
{
set
{
m_Remark = value;
}
get
{
return m_Remark;
}
}
public virtual string SortFieldName
{
get
{
if(m_SortFieldName == null)
{
m_SortFieldName = "ID";
}
return m_SortFieldName;
}
set
{
m_SortFieldName = value;
}
}
/// <summary>
/// 数据库代理对象(对数据库的操作)
/// </summary>
public virtual IDataProxy DataProxy
{
get
{
return this.m_DataProxy;
}
set
{
this.m_DataProxy = value;
}
}
public bool IsModify
{
get
{
return this.m_IsModify;
}
set
{
this.m_IsModify = value;
}
}
public bool IsDirty
{
get
{
return this.m_IsDirty;
}
set
{
this.m_IsDirty = value;
}
}
#endregion
#region 构造器
/// <summary>
/// 构造器
/// </summary>
public BaseObject()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 带数据代理对象的构造器
/// </summary>
/// <param name="objDataProxy"></param>
public BaseObject(IDataProxy objDataProxy)
{
this.DataProxy = objDataProxy;
}
#endregion
#region 私有方法
#endregion
#region 公共方法
public static BaseObject New()
{
return new BaseObject();
}
/// <summary>
/// 从数据库中装载数据
/// </summary>
/// <returns>对象的一个新实例</returns>
public BaseObject Load(int id)
{
return DataProxy.GetItemByID(id);
}
public virtual void Update()
{
if (this.IsModify)
{
DataProxy.Update(this);
}
}
public virtual void Delete(int id)
{
DataProxy.DeleteByID(id);
}
#endregion
#region IComparable 成员
public virtual int CompareTo(object obj)
{
if (SortFieldName.ToUpper().Equals("ID"))
{
return ID.CompareTo(((BaseObject)obj).ID);
}
if(SortFieldName.ToUpper().Equals("TITLE"))
{
if (Title == null)
{
return -1;
}
return this.Title.CompareTo(((BaseObject)obj).Title);
}
return 0;
}
#endregion
#region 重载方法
/// <summary>
/// 比较两个对象是否相等
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
return ID.Equals(((BaseObject)obj).ID);
}
public override int GetHashCode()
{
return ID.GetHashCode ();
}
#endregion
#region 操作符重栽
public static bool operator ==(BaseObject x, BaseObject y)
{
return x.ID == y.ID;
}
public static bool operator !=(BaseObject x, BaseObject y)
{
return !(x == y);
}
#endregion
public override string ToString()
{
return this.Title;
}
}
}
数据实体对象News
using System;
using System.Data;
using VUser.ECommerce.DataProxy;
namespace VUser.ECommerce.Core
{
/// <summary>
/// News 的摘要说明。
/// </summary>
public class News : BaseObject
{
private DateTime m_AddTime = DateTime.Now;
private int m_ClassID;
private string m_ClassName;
private NewClass m_NewClassObj;
private bool m_IsPublish;
public DateTime AddTime
{
get
{
return m_AddTime;
}
set
{
m_AddTime = value;
}
}
public int ClassID
{
get
{
return m_ClassID;
}
set
{
// if (!new NewsCollection().IsExist(value))
// {
// throw new Exception("the ClassID isn't exist");
// }
m_ClassID = value;
}
}
public string ClassName
{
get
{
return this.m_ClassName;
}
set
{
this.m_ClassName = value;
}
}
public NewClass NewClassObj
{
get
{
if(m_NewClassObj == null)
{
if(this.m_ClassID != 0)
{
m_NewClassObj = (NewClass)(new NewClass().Load(ClassID));
}
}
return m_NewClassObj;
}
}
public bool IsPublish
{
get
{
return m_IsPublish;
}
set
{
m_IsPublish = value;
}
}
public News()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public override int CompareTo(object obj)
{
if (this.SortFieldName.ToUpper().Equals("AddTime"))
{
return AddTime.CompareTo(((News)obj).AddTime);
}
if (this.SortFieldName.ToUpper().Equals("ClassID"))
{
return ClassID.CompareTo(((News)obj).ClassID);
}
if (this.SortFieldName.ToUpper().Equals("ClassName"))
{
return ClassName.CompareTo(((News)obj).ClassName);
}
if (this.SortFieldName.ToUpper().Equals("IsPublish"))
{
return IsPublish.CompareTo(((News)obj).IsPublish);
}
return base.CompareTo (obj);
}
}
}
数据集合对象
using System;
using System.Collections;
using System.Data;
using VUser.ECommerce.DataProxy;
namespace VUser.ECommerce.Core
{
[Serializable]
public class BaseCollection : IEnumerator,ICollection
{
#region 域
#region 数据同步标识位
/// <summary>
/// Items(内存)数据是否更改过,还没有提交到数据库
/// </summary>
private bool m_IsModify = false;
/// <summary>
/// 是否数据库中的数据已经更改,Items(内存)数据已经"脏"了
/// </summary>
private bool m_IsDirty = false;
#endregion
/// <summary>
/// 浏览数据的游标指针
/// </summary>
private int m_Index = -1;
/// <summary>
/// 操作数据库的代理对象
/// </summary>
private IDataProxy m_DataProxy;
/// <summary>
/// 存储数据对象
/// </summary>
private ArrayList m_Items;
#endregion
#region 属性
/// <summary>
/// set sort fields name
/// </summary>
public string SetSortFieldName
{
set
{
for(int i = 0 ;i< Items.Count;i++)
{
((BaseObject)Items[i]).SortFieldName = value;
}
}
}
/// <summary>
/// Items(内存)数据是否更改过,还没有提交到数据库
/// </summary>
public bool IsModify
{
get
{
return m_IsModify;
}
set
{
m_IsModify = value;
}
}
/// <summary>
/// 是否数据库中的数据已经更改,Items(内存)数据已经"脏"了
/// </summary>
public bool IsDirty
{
set
{
m_IsDirty = value;
}
get
{
return m_IsDirty;
}
}
/// <summary>
/// 记录的集合
/// </summary>
public ArrayList Items
{
get
{
if(this.m_Items == null)
{
m_Items = new ArrayList();
}
return this.m_Items;
}
}
/// <summary>
/// 集合对象中的记录数量
/// </summary>
public int Count
{
get
{
return Items.Count;
}
}
/// <summary>
/// 操作数据库的代理对象
/// </summary>
public virtual IDataProxy DataProxy
{
get
{
return m_DataProxy;
}
set
{
m_DataProxy = value;
}
}
/// <summary>
/// 索引器
/// </summary>
public BaseObject this[int index]
{
get
{
if (Items != null)
{
if ((index >=0) || ( index < this.Items.Count))
return (BaseObject)Items[index];
else
return null;
}
else
{
return null;
}
}
set
{
if (this.Items != null)
{
if ((index >=0) || ( index < this.Items.Count))
this.Items[index] = value;
}
}
}
#endregion
#region 构造器
public BaseCollection()
{
}
private BaseCollection(ArrayList items)
{
m_Items = items;
}
#endregion
#region 利用数据库代理对象(DataProxy)的操作
#region 从数据库中装载数据
public virtual BaseCollection Load()
{
return DataProxy.Load();
}
#endregion
public void DeleteByID(int id)
{
DataProxy.DeleteByID(id);
}
public virtual int Add(BaseObject objData)
{
return DataProxy.Add(objData);
}
public virtual bool IsExist(int id)
{
return this.DataProxy.IsExist(id);
}
public virtual BaseCollection GetItems()
{
return this.DataProxy.Load();
}
public virtual DataSet ToDataSet()
{
return this.DataProxy.ToDataSet();
}
#endregion
#region ICollection 成员
public bool IsSynchronized
{
get
{
// TODO: 添加 EOCollection.IsSynchronized getter 实现
return false;
}
}
public void CopyTo(Array array, int index)
{
// TODO: 添加 EOCollection.CopyTo 实现
}
public object SyncRoot
{
get
{
// TODO: 添加 EOCollection.SyncRoot getter 实现
return null;
}
}
#endregion
#region IEnumerable 成员
public virtual IEnumerator GetEnumerator()
{
// TODO: 添加 EOCollection.GetEnumerator 实现
//return (IEnumerator) new EOCollection(this.Items);
return this.Items.GetEnumerator();
}
public object Current
{
get
{
return Items[m_Index];
}
}
public bool MoveNext()
{
m_Index++;
return m_Index < Items.Count;
}
public void Reset()
{
m_Index = -1;
}
#endregion
#region 内存数据操作
/// <summary>
/// 添加Items集合的数据(到内存,暂时不提交到数据库)
/// </summary>
/// <param name="obj"></param>
public void AddToMemo(BaseObject obj)
{
Items.Add(obj);
}
/// <summary>
/// 删除items集合中的一行记录
/// </summary>
/// <param name="obj">按对象的ID删除</param>
public void DeleteFromMemo(BaseObject obj)
{
Items.Remove(obj);
}
/// <summary>
/// 删除items集合中的一行记录
/// </summary>
/// <param name="id">按对象的ID删除</param>
public void DeleteFromMemo(int id)
{
BaseObject obj = BaseObject.New();
obj.ID = id;
DeleteFromMemo(obj);
}
/// <summary>
/// 对items集合指定的字段排序
/// </summary>
/// <param name="fieldName">要排序的字段名称</param>
public virtual void Sort(string fieldName)
{
Items.Sort(new BaseSort(fieldName));
}
public virtual void Sort()
{
Items.Sort();
}
public virtual void Sort(bool isDesc)
{
Items.Sort();
if (isDesc)
{
Items.Reverse();
}
}
/// <summary>
/// 对items集合指定的字段排序
/// </summary>
/// <param name="fieldName">要排序的字段名称</param>
/// <param name="isDesc">是否降序排列</param>
public virtual void Sort(string fieldName,bool isDesc)
{
Sort(fieldName);
if (isDesc)
{
Items.Reverse();
}
}
#endregion
#region 检索内存中的对象
public int IndexOf(object obj)
{
return Items.IndexOf(obj);
}
#endregion
#region 数据库内存交互操作
public virtual void Refresh()
{
}
public virtual void Submit()
{
if (IsModify)
{
foreach(BaseObject obj in this.Items)
{
if (obj.IsModify)
{
this.DataProxy.Update(obj);
}
}
}
}
#endregion
}
}
排序对象:
using System;
using System.Collections;
using System.Reflection;
namespace VUser.ECommerce.Core
{
/// <summary>
/// BaseSort 的摘要说明。
/// </summary>
public class BaseSort : IComparer
{
private string m_SortFieldName;
public BaseSort(string fieldName)
{
m_SortFieldName = fieldName;
}
public virtual int CompareField(object x,object y)
{
try
{
Type typex = x.GetType();
Type typey = y.GetType();
//Get each property by name
PropertyInfo pix = typex.GetProperty(m_SortFieldName);
PropertyInfo piy = typey.GetProperty(m_SortFieldName);
//Get the value of the property for each object
IComparable pvalx = (IComparable)pix.GetValue(x, null);
object pvaly = piy.GetValue(y, null);
//Compare values, using IComparable interface of the property's type
return pvalx.CompareTo(pvaly);
}
catch// if x or y is null value then return -1
{
return -1;
}
}
#region IComparer 成员
public virtual int Compare(object x, object y)
{
// TODO: 添加 BaseSort.Compare 实现
return CompareField(x,y);
}
#endregion
}
}
应用示例:
排序集合示例(1)
public void DataBindControl(int pagei)
{
NewClassDataProxy obj = new NewClassDataProxy();
NewClassCollection coll = (NewClassCollection)(obj.GetItems());
//调用BaseSort 对象进行排序
coll.Sort("ID");
or//coll.Sort(“Title“);
this.DataGrid1.DataSource = (NewClassCollection)coll;
this.DataGrid1.CurrentPageIndex = pagei;
this.DataGrid1.DataBind();
}
\排序集合示例(2)
private void Button3_Click(object sender, System.EventArgs e)
{
NewClassDataProxy obj = new NewClassDataProxy();
NewClassCollection coll = (NewClassCollection)(obj.GetItems());
//调用默认的定义排序接口实现(IComparable)
coll.SetSortFieldName = "ID";
coll.Sort();
this.DataGrid1.DataSource = (NewClassCollection)coll;
this.DataGrid1.DataBind();
}