我自己记录的 用 DataTable 来做实体的 ORM 原型

利用 DataTable ,实现 ORM
DataTable 可以添加,但不方便,如果只添加一部分,如 只添加主键.则要添加全部.
DataTable 的更新还是很方便的.
DataTable 没有选择结果集,要手动添加上.
DataTable 没有根据主键选择行.添加上.另外,返回 DataRow的指针.


Rose 里有没有 .NET 的插件.




public class PrimaryKey : Attribute
    {

    }

    public class OrmTable : DataTable
    {
        protected virtual object GetThis()
        {
            return this ;
        }

        public DataRow SelectRow()
        {
            DB_EquipType et = GetThis() as DB_EquipType ;
            et.Rows.Clear() ;
            Sys.DBFact.thisOne.GetDataTable(" select * from equiptype where id = 1 " ,(DataTable )et ) ;

            return et.Rows[0] ;
        }

        public DataRow[] GetRows()
        {
            return base.Select() ;
        }

        public new void Select()
        {
        }

        public void DeleteRow()
        {
            Sys.DBFact.thisOne.GetDataTable(" select * from equiptype where id = 1 " ,(DataTable ) GetThis() ) ;
        }

        public void UpdateRow()
        {

        }

        /// <summary>
        /// 根据状态,执行更新或插入.
        /// </summary>
        public void Save()
        {
             //如果主键改变,则根据数据库情况,如果存在该主键行.则更新,否则插入.
        }

        public void Insert()
        {
        }

        public void Update()
        {
        }
     }

    public class DB_EquipType : OrmTable
    {
        protected override object GetThis()
        {
            return this;
        }

         public DB_EquipType(int ID)
        {
            this.id = ID ;
            SelectRow() ;
        }

        private int _id ;
        [PrimaryKey]
        public int id
        {
            get
            {
                return this._id ;
            }
            set
            {
                this._id = value ;
            }
        }

        private string _val ;
        public string val
        {
            get
            {
                return this._val ;
            }
            set
            {
                this._val = value ;
            }
        }
        private int _pid ;
        public int pid
        {
            get
            {
                return this._pid ;
            }
            set
            {
                this._pid = value ;
            }
        }
        private int _sortid ;
        public int sortid
        {
            get
            {
                return this._sortid ;
            }
            set
            {
                this._sortid = value ;
            }
        }
    }

posted @ 2009-03-09 00:59  NewSea  阅读(428)  评论(0)    收藏  举报