小结面向对象的类的设计方式

时间:2022-09-24 20:40:18

开始接触。NET的人都会知道。NET是面向对象的编程,即使是从ASP往ASP。NET转型的人也会发现,尽管代码页面还是熟悉的HTML页,而后置页却也是一个继承于 System.Web.UI.Page的类。在类中实现了3个方法:
Page_Load(object sender, System.EventArgs e)
OnInit(EventArgs e)
 InitializeComponent()

代码后置页主要是实现业务层的功能,实现对数据有效性,业务逻辑等方面的功能。而肩负着对数据库桥梁的则是自己定义的数据实体类,通常我们都是定义一个只包含某对象的属性的类,
如下: 实体类:DatfaultInfo,该类封装了相关属性。

using System;
namespace oilManage
{
    public class DatfaultInfo
    {
        public DatfaultInfo()
        {
        }
        public DatfaultInfo(string descrption,string dispose,decimal fault_id,DateTime operatedate,string operateuser,decimal trialinfo_id)
        {
            this.Descrption=descrption;
            this.Dispose=dispose;
            this.Fault_id=fault_id;
            this.Operatedate=operatedate;
            this.Operateuser=operateuser;
            this.Trialinfo_id=trialinfo_id;
        }
        private string mDescrption;
        public string Descrption
        {
            get{ return mDescrption ;}
            set{ mDescrption=value ;}
        }
        private string mDispose;
        public string Dispose
        {
            get{ return mDispose ;}
            set{ mDispose=value ;}
        }
        private decimal mFault_id;
        public decimal Fault_id
        {
            get{ return mFault_id ;}
            set{ mFault_id=value ;}
        }
        private DateTime mOperatedate;
        public DateTime Operatedate
        {
            get{ return mOperatedate ;}
            set{ mOperatedate=value ;}
        }
        private string mOperateuser;
        public string Operateuser
        {
            get{ return mOperateuser ;}
            set{ mOperateuser=value ;}
        }
        private decimal mTrialinfo_id;
        public decimal Trialinfo_id
        {
            get{ return mTrialinfo_id ;}
            set{ mTrialinfo_id=value ;}
        }
    }
}

对于自定义的类,考虑到要提供集合的功能,以便于实现各种接口,如:IList,ICollection,IEnumerable 方面访问集合,于是又利用arrayList实现了对该数据类的集合类。
如下:DatfaultInfoCollection

using System;
using System.Collections;
namespace oilManage
{
    public class DatfaultInfoCollection:IList,ICollection,IEnumerable
    {
        System.Collections.ArrayList elements;
        public DatfaultInfoCollection()
        {
            elements=new System.Collections.ArrayList();
        }

        public DatfaultInfo this[int index]
        {
            get
            {
                return (DatfaultInfo)this.elements[index];
            }
            set
            {
                this.elements[index]=value;
            }
        }

        int IList.IndexOf(object value)
        {
            return ((DatfaultInfoCollection)this).IndexOf((DatfaultInfo)value);
        }

        public int IndexOf(DatfaultInfo value)
        {
            return this.elements.IndexOf(value);
        }

        public bool IsReadOnly
        {
            get
            {
                return this.elements.IsReadOnly;
            }
        }

        object IList.this[int index]
        {
            get
            {
                return ((DatfaultInfoCollection)this)[index];
            }
            set
            {
                ((DatfaultInfoCollection)this)[index]=(DatfaultInfo)value;
            }
        }

        public bool IsFixedSize
        {
            get
            {
                return this.elements.IsSynchronized;
            }
        }

        public bool IsSynchronized
        {
            get
            {
                return true;
            }
        }

        public int Count
        {
            get
            {
                return this.elements.Count;
            }
        }

        public void CopyTo(System.Array array,int index)
        {
            this.elements.CopyTo(array,index);
        }

        public object SyncRoot
        {
            get
            {
                return this.elements.SyncRoot;
            }
        }

        public System.Collections.IEnumerator GetEnumerator()
        {
            return this.elements.GetEnumerator();
        }

        public int Add(DatfaultInfo value)
        {
            return this.elements.Add(value);
        }

        int IList.Add(object value)
        {
            return ((DatfaultInfoCollection)this).Add((DatfaultInfo)value);
        }

        void IList.Insert(int index, object value)
        {
            ((DatfaultInfoCollection)this).Insert(index,(DatfaultInfo)value);
        }

        public void Insert(int index, DatfaultInfo value)
        {
            this.elements.Insert(index,value);
        }

        public void RemoveAt(int index)
        {
            this.elements.RemoveAt(index);
        }

        void IList.Remove(object value)
        {
            ((DatfaultInfoCollection)this).Remove((DatfaultInfo)value);
        }

        public void Remove(DatfaultInfo value)
        {
            this.elements.Remove(value);
        }

        bool IList.Contains(object value)
        {
            return ((DatfaultInfoCollection)this).Contains((DatfaultInfo)value);
        }

        public bool Contains(DatfaultInfo value)
        {
            return this.elements.Contains(value);
        }

        public void Clear()
        {
            this.elements.Clear();
        }
    }
}

对于给定的数据我们都用加上一些与数据库通信的CRUD方法。
于是我们对于其定义了数据访问类:DatfaultDB
  

using System;
using System.Data;
using System.Data.OracleClient;
using Fjeptri.DataAccess.DataHelper;
namespace oilManage
{
    public class DatfaultDB
    {
        public static bool Create(DatfaultInfo datfaultInfo)
        {
            string CREATE="insert into DATFAULT(DESCRPTION,DISPOSE,FAULT_ID,OPERATEDATE,OPERATEUSER,TRIALINFO_ID) values(:DESCRPTION,:DISPOSE,:FAULT_ID,:OPERATEDATE,:OPERATEUSER,:TRIALINFO_ID)";
            OracleParameter[] param=OracleHelper.GetCacheParameter(CREATE);
            if(param==null)
            {
                param=new OracleParameter[6];
                param[0]=new OracleParameter("DESCRPTION",OracleType.VarChar,4000);
                param[1]=new OracleParameter("DISPOSE",OracleType.VarChar,4000);
                param[2]=new OracleParameter("FAULT_ID",OracleType.Number);
                param[3]=new OracleParameter("OPERATEDATE",OracleType.DateTime);
                param[4]=new OracleParameter("OPERATEUSER",OracleType.VarChar,20);
                param[5]=new OracleParameter("TRIALINFO_ID",OracleType.Number);
                OracleHelper.CacheParameter(CREATE,param);
            }
            param[0].Value=datfaultInfo.Descrption==null?string.Empty:datfaultInfo.Descrption;
            param[1].Value=datfaultInfo.Dispose==null?string.Empty:datfaultInfo.Dispose;
            param[2].Value=datfaultInfo.Fault_id;
            param[3].Value=datfaultInfo.Operatedate;
            param[4].Value=datfaultInfo.Operateuser==null?string.Empty:datfaultInfo.Operateuser;
            param[5].Value=datfaultInfo.Trialinfo_id;
            return OracleHelper.ExecuteNonQuery(ConfigInfo.GetConnectionString(),CommandType.Text,CREATE,param)>0?true:false;
        }
        public static int Delete(DatfaultPK datfaultPK)
        {
            string DELETE="delete DATFAULT where FAULT_ID=:FAULT_ID";
            OracleParameter[] param=OracleHelper.GetCacheParameter(DELETE);
            if(param==null)
            {
                param=new OracleParameter[1];
                param[0]=new OracleParameter("FAULT_ID",OracleType.Number);
                OracleHelper.CacheParameter(DELETE,param);
            }
            param[0].Value=datfaultPK.Fault_id;
            return OracleHelper.ExecuteNonQuery(ConfigInfo.GetConnectionString(),CommandType.Text,DELETE,param);
        }
        public static DatfaultInfo Read(DatfaultPK datfaultPK)
        {
            string READ="select DESCRPTION,DISPOSE,FAULT_ID,OPERATEDATE,OPERATEUSER,TRIALINFO_ID from DATFAULT where FAULT_ID=:FAULT_ID";
            OracleParameter[] param=OracleHelper.GetCacheParameter(READ);
            if(param==null)
            {
                param=new OracleParameter[1];
                param[0]=new OracleParameter("FAULT_ID",OracleType.Number);
                OracleHelper.CacheParameter(READ,param);
            }
            param[0].Value=datfaultPK.Fault_id;
            using(OracleDataReader dr=OracleHelper.ExecuteReader(ConfigInfo.GetConnectionString(),CommandType.Text,READ,param))
            {
                if(dr.Read()==false)
                    return null;
                DatfaultInfo obj=new DatfaultInfo();
                if(!dr.IsDBNull(0))
                    obj.Descrption=dr.GetString(0);
                if(!dr.IsDBNull(1))
                    obj.Dispose=dr.GetString(1);
                if(!dr.IsDBNull(2))
                    obj.Fault_id=dr.GetDecimal(2);
                if(!dr.IsDBNull(3))
                    obj.Operatedate=dr.GetDateTime(3);
                if(!dr.IsDBNull(4))
                    obj.Operateuser=dr.GetString(4);
                if(!dr.IsDBNull(5))
                    obj.Trialinfo_id=dr.GetDecimal(5);
                return obj;
            }
        }
        public static DataTable SelectAllDatfaultInfo()
        {
            string SELECTALLDATFAULTINFO="select DESCRPTION,DISPOSE,FAULT_ID,OPERATEDATE,OPERATEUSER,TRIALINFO_ID from DATFAULT";
            return OracleHelper.ExecuteDataTable(ConfigInfo.GetConnectionString(),CommandType.Text,SELECTALLDATFAULTINFO);
        }
        public static bool Update(DatfaultInfo datfaultInfo)
        {
            string UPDATE="update DATFAULT set DESCRPTION=:DESCRPTION,DISPOSE=:DISPOSE,FAULT_ID=:FAULT_ID,OPERATEDATE=:OPERATEDATE,OPERATEUSER=:OPERATEUSER,TRIALINFO_ID=:TRIALINFO_ID where FAULT_ID=:FAULT_ID";
            OracleParameter[] param=OracleHelper.GetCacheParameter(UPDATE);
            if(param==null)
            {
                param=new OracleParameter[7];
                param[0]=new OracleParameter("DESCRPTION",OracleType.VarChar,4000);
                param[1]=new OracleParameter("DISPOSE",OracleType.VarChar,4000);
                param[2]=new OracleParameter("FAULT_ID",OracleType.Number);
                param[3]=new OracleParameter("OPERATEDATE",OracleType.DateTime);
                param[4]=new OracleParameter("OPERATEUSER",OracleType.VarChar,20);
                param[5]=new OracleParameter("TRIALINFO_ID",OracleType.Number);
                param[6]=new OracleParameter("FAULT_ID",OracleType.Number);
                OracleHelper.CacheParameter(UPDATE,param);
            }
            param[0].Value=datfaultInfo.Descrption==null?string.Empty:datfaultInfo.Descrption;
            param[1].Value=datfaultInfo.Dispose==null?string.Empty:datfaultInfo.Dispose;
            param[2].Value=datfaultInfo.Fault_id;
            param[3].Value=datfaultInfo.Operatedate;
            param[4].Value=datfaultInfo.Operateuser==null?string.Empty:datfaultInfo.Operateuser;
            param[5].Value=datfaultInfo.Trialinfo_id;
            param[6].Value=datfaultInfo.Fault_id;
            return OracleHelper.ExecuteNonQuery(ConfigInfo.GetConnectionString(),CommandType.Text,UPDATE,param)>0?true:false;
        }
    }
}

在这个数据访问类中包含了简单的CRUD四个静态方法,因为都是静态方法,所以在项目中可以把整个项目的数据访问类都放在同个抽象类中,以便于管理,同时也便于数据库的移植。