数据库表间关系管理原理(2)

原创 2007年09月30日 20:58:00

这个是源码

-------------------------- 基本数据接口-------------------

namespace XPDataAccess
{
    /// <summary>
    /// 关键字接口
    /// </summary>
    public interface IParmaryKey<P>
    {
        /// <summary>
        /// 关键字
        /// </summary>
        P  PrimaryKey
        {
            get ;
        }        
    }
    /// <summary>
    /// 显示注册状态的参数
    /// </summary>
    public enum   RegisterRelationStatus
    {
        /// <summary>
        /// 注册成功
        /// </summary>
        RegistorOk,
        /// <summary>
        /// 关系已经存在
        /// </summary>
        RelationExist,
        /// <summary>
        /// 第一个主键不存在
        /// </summary>
        FirstKeyNotFound,
        /// <summary>
        /// 第二个主键不存在
        /// </summary>
        SecondKeyNotFound
    }
    /// <summary>
    /// 关系管理器注册事件参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="U"></typeparam>
    public class RegisterEventArgs<T, U> : EventArgs
    {
        private T _t;
        private U _u;

        private RegisterRelationStatus _st;
        public RegisterEventArgs(T t, U u,RegisterRelationStatus st)
        {
            _t = t;
            _u = u;
            _st =st ;
        }
        public T First
        {
            get { return _t; }
        }
     
        public U Second
        {
            get { return _u; }
        }
      
        public RegisterRelationStatus RegisterStatus
        {
            get { return _st ;}
        }
    }

//数据库关系管理的基类,1:1,1:M,M:N的关系的管理均从此类继承.

 public abstract  class Relation<T,U,Tp,Up>  where T:class ,IParmaryKey<Tp>  where U:class ,IParmaryKey<Up>
    {
       protected Relation()
       {
       }
        #region IRelation<T,U> Members

        public event EventHandler<RegisterEventArgs<T, U>> Registered;

        public event EventHandler<RegisterEventArgs<T, U>> UnRegistered;

        public void  Register(T t, U u)
        {
            RegisterRelationStatus status = AddRelation(t.PrimaryKey, u.PrimaryKey);

            OnRegistered(new RegisterEventArgs<T, U>(t, u, status));
           
        }

        public void UnRegister(T t, U u)
        {
            RegisterRelationStatus status = RemoveRelation(t.PrimaryKey, u.PrimaryKey);
            OnUnRegistered(new RegisterEventArgs<T, U>(t, u, status));

        }

        #endregion
       protected virtual void OnRegistered(RegisterEventArgs<T, U> e)
       {
           if (Registered != null)
           {
               Registered(this, e);
           }
       }
       protected virtual void OnUnRegistered(RegisterEventArgs<T, U> e)
       {
           if (UnRegistered != null)
           {
               UnRegistered(this, e);
           }

       }
       protected abstract RegisterRelationStatus AddRelation(Tp t, Up u);
       protected abstract RegisterRelationStatus RemoveRelation(Tp t, Up u);
       public abstract T FindT(Tp tp) ;
       public abstract U FindU(Up key) ;
    }

    /// <summary>
    /// 一对一关系的相互映射接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="U"></typeparam>
    /// <typeparam name="Tp"></typeparam>
    /// <typeparam name="Up"></typeparam>
    public interface IOneToOne<T,U,Tp,Up> where T:class ,IParmaryKey<Tp> where U:class ,IParmaryKey<Up>
    {
        T FindOtherOne(U u);
        U FindOtherOne(T t);
       
    }
    /// <summary>
    /// 一对一关系的管理基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="U"></typeparam>
    /// <typeparam name="Tp"></typeparam>
    /// <typeparam name="Up"></typeparam>
    public abstract class   OneToOneRelation<T, U, Tp, Up> : Relation<T, U, Tp, Up>,IOneToOne<T,U,Tp,Up>
        where T : class, IParmaryKey<Tp>
        where U : class, IParmaryKey<Up>
    {
        protected abstract override  RegisterRelationStatus AddRelation(Tp t, Up u);

        protected abstract override  RegisterRelationStatus RemoveRelation(Tp t, Up u);

        public abstract override T FindT(Tp tp);
        public abstract override U FindU(Up key);

        #region IOneToOne<T,U,Tp,Up> Members

        public abstract T FindOtherOne(U u);
        public abstract U FindOtherOne(T t);

        #endregion
    }
    /// <summary>
    /// 宪法
    /// </summary>
    public class Law : IParmaryKey<int>
    {
        public Law()
        {
        }
        private int _lowId;
        private string _author;

        /// <summary>
        /// 宪法的起草人
        /// </summary>
        public string Author
        {
            get { return _author; }
            set { _author = value; }
        }
        #region IParmaryKey<int> Members

        public int PrimaryKey
        {
            get { return _lowId; }
            set { _lowId = value; }
        }


        #endregion
    }

    /// <summary>
    /// 国家
    /// </summary>
    public class Country : IParmaryKey<int>
    {
        public Country()
        {
        }
        private int _cid;
        private string _name;
        private DateTime _jgTime;

        public DateTime Gen
        {
            get { return _jgTime; }
            set { _jgTime = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
       
        #region IParmaryKey<int> Members

        public int PrimaryKey
        {
            get { return _cid; }
            set { _cid = value; }
        }
       

        #endregion
    }

//1:1关系的用例.

    public class CountryLaw : OneToOneRelation<Country, Law, int, int>
    {

        protected override RegisterRelationStatus AddRelation(int t, int u)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        protected override RegisterRelationStatus RemoveRelation(int t, int u)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override Country FindT(int tp)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override Law FindU(int key)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override Country FindOtherOne(Law u)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override Law FindOtherOne(Country t)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }

   ///////////////////////////////////////1:M关系/////////////////////////////

 public interface IOneToMany<T,U,Tp,Up> where T:class ,IParmaryKey<Tp> where U:class ,IParmaryKey<Up>
    {
      
        T     Parent(U u);
        U[]   Subs(T u);
       
    }
    /// <summary>
    /// 一对多关系管理的基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="U"></typeparam>
    /// <typeparam name="Tp"></typeparam>
    /// <typeparam name="Up"></typeparam>
    public abstract class OneToManyRelation<T, U, Tp, Up> : Relation<T, U, Tp, Up>, IOneToMany<T, U, Tp, Up>
        where T : class, IParmaryKey<Tp>
        where U : class, IParmaryKey<Up>
    {
        protected abstract override RegisterRelationStatus AddRelation(Tp t, Up u);

        protected abstract override RegisterRelationStatus RemoveRelation(Tp t, Up u);

        public abstract override T FindT(Tp tp);
        public abstract override U FindU(Up key);


        #region IOneToMany<T,U,Tp,Up> Members

        public abstract  T Parent(U u);

        public abstract  U[] Subs(T u);

        #endregion
    }
    /// <summary>
    /// 学校
    /// </summary>
    public class School:IParmaryKey<Int32>
    {
        public School()
        {
        }
        private int _sid;
        private  string _sn;
        private  string _addr;

        public string SchoolName
        {
            get { return _sn; }
            set { _sn = value; }
        }
        public string SchoolAddress
        {
            get { return _addr; }
            set { _addr = value; }
        }

        #region IParmaryKey<int> Members

        public int PrimaryKey
        {
            get { return _sid; }
            set { _sid = value; }
        }

        #endregion
    }
    /// <summary>
    /// 老师
    /// </summary>
    public class Teacher : IParmaryKey<Guid>
    {

        private  string _tn;
        private  Guid _uid;
        public Teacher()
        {
        }

        public string TeacherName
        {
            get { return _tn; }
            set { _tn = value; }
        }

     
        #region IParmaryKey<Guid> Members

        public Guid PrimaryKey
        {
            get { return _uid; }
            set { _uid = value; }
        }

        #endregion
    }


    public class SchoolTeacher : OneToManyRelation<School, Teacher, int, Guid>
    {
        protected override RegisterRelationStatus AddRelation(int t, Guid u)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        protected override RegisterRelationStatus RemoveRelation(int t, Guid u)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override School FindT(int tp)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override Teacher FindU(Guid key)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override School Parent(Teacher u)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override Teacher[] Subs(School u)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }

  ////////////////////////////////M:N关系///////////////////////////////////////

 /// <summary>
    /// 多对多关系的接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="U"></typeparam>
    /// <typeparam name="Tp"></typeparam>
    /// <typeparam name="Up"></typeparam>
    public interface IManyToMany<T, U, Tp, Up>
        where T : class, IParmaryKey<Tp>
        where U : class, IParmaryKey<Up>
    {

        T[] Subs(U u);
        U[] Subs(T u);

    }
    /// <summary>
    /// 多对多关系管理的基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="U"></typeparam>
    /// <typeparam name="Tp"></typeparam>
    /// <typeparam name="Up"></typeparam>
    public abstract class ManyToManyRelation<T, U, Tp, Up> : Relation<T, U, Tp, Up>, IManyToMany<T, U, Tp, Up>
        where T : class, IParmaryKey<Tp>
        where U : class, IParmaryKey<Up>
    {
        protected abstract override RegisterRelationStatus AddRelation(Tp t, Up u);

        protected abstract override RegisterRelationStatus RemoveRelation(Tp t, Up u);

        public abstract override T FindT(Tp tp);
        public abstract override U FindU(Up key);

 

        #region IManyToMany<T,U,Tp,Up> Members

        public abstract  T[] Subs(U u);

        public abstract  U[] Subs(T u);

        #endregion
    }

    public class User : IParmaryKey<Guid>
    {
        private string _rn;
        private string _app;
        private Guid _rid;
        public string UserName
        {
            get { return _rn; }
            set { _rn = value; }
        }
        public string RealName
        {
            get { return _app; }
            set { _app = value; }
        }
        #region IParmaryKey<Guid> Members

        public Guid PrimaryKey
        {
            get { return _rid; }
            set { _rid = value; }
        }

        #endregion
    }
    public class Roles : IParmaryKey<Guid>
    {
        public Roles()
        {

        }
        private string _rn;
        private string _app;
        private Guid _rid;
        public string RoleName
        {
            get { return _rn; }
            set { _rn = value; }
        }
        public string Description
        {
            get { return _app; }
            set { _app = value; }
        }
        #region IParmaryKey<Guid> Members

        public Guid PrimaryKey
        {
            get { return _rid; }
            set { _rid = value; }
        }

        #endregion
    }


    public class UserRoles : ManyToManyRelation<User, Roles, Guid, Guid>
    {
        protected override RegisterRelationStatus AddRelation(Guid t, Guid u)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        protected override RegisterRelationStatus RemoveRelation(Guid t, Guid u)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override User FindT(Guid tp)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override Roles FindU(Guid key)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override User[] Subs(Roles u)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override Roles[] Subs(User u)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }

 
}

----------------------------------------------以后就是基于1:1,1:M,M:N三种基本的数据关系的展示和用例.

在应用到www.studyez.com的DAL层与BL层的开发上面,结合 ObjectDataSourse控件及  通用的分页解决方案 ,取得比较好的效果

数据库表间关系管理原理

公司的网站www.studyez.com 在实际开发过程中,经常要对数据库进行操作,搞来搞去常用的也就哪么几种。所以就想把数据库的操作进行一些封装。记得在那本书或是杂志上看到某个牛人这样评价数据库的设...
  • dhz123
  • dhz123
  • 2007年09月30日 19:49
  • 737

数据库表间关系

1.一对一关系Ex:身份证表与用户表 在身份证表中添加 用户表中的主键栏位作为 身份证表中的外间和主键 这样就保证了 两表之间数据的关联和唯一性 CREATE TABLE `card` ( ...
  • qq_31179919
  • qq_31179919
  • 2017年02月16日 15:49
  • 99

数据库设计 表和表之间的三种关系

表与表之间一般存在三种关系,即一对一,一对多,多对多关系。  下面分别就三种关系讲解数据库相关设计的思路和思考过程; (1)一对一关系  例如,下面的一张表,保存了人的相关信息,有男有女,要...
  • qq_33574890
  • qq_33574890
  • 2017年12月15日 17:29
  • 186

[团队管理]从《亮剑》看团队建设之二——PM如何与组员合作

关于团队组建大家都有一个共同的认识,一个项目组,必然有一个带队的,称为PM。PM如何和组员合作,这是一个争议较多的话题,有人说与手下人如何合作?手下人不干活怎么办?团队成员间关系的协调等等。项目经理与...
  • qingrun
  • qingrun
  • 2006年11月14日 15:55
  • 5487

用友U852 从后台数据库反启用资金管理

用友U852 从后台数据库反启用资金管理 无法直接用账套主管反启用了,可以考虑从后台数据库反启用,修改两个表: 账套库中的Accinformation、系统库的UA_accountsub sel...
  • tlsopen
  • tlsopen
  • 2015年03月31日 15:39
  • 927

第五章 管理数据库表

用来创建表的sql语句称为数据库定义语言DDL DDL全称 data definitation language 主要用来操作 oracle数据库的结构。 create index;创建数据表索引 ...
  • a7459
  • a7459
  • 2016年11月26日 19:36
  • 119

EA基础篇ER图生成数据库表

EA ER ER图生成数据表
  • sky15732625340
  • sky15732625340
  • 2016年08月07日 16:34
  • 2252

数据库表管理

数据的完整性=精确性+可靠性 数据丧失完整性体现在:数据可中存在不符合规定的数据或错误的信息,例如:学号重复,身份证号重复,年龄为负数,薪水为负数,性别不存在 数据丧失完整性:是在设计表结构的时候...
  • vera514514
  • vera514514
  • 2012年07月21日 21:53
  • 742

数据库中的数据表之间的关系

主键:能够唯一表示数据表中的每个记录的字段或者字段的组合就称为主键。一个主键是唯一识别一个表的每一行记录,但这只是其作用的一疗分,主键的主要作用是将记录和存放在其他表中的数据进行关联,在这一点上,主键...
  • normal_cai
  • normal_cai
  • 2012年12月14日 16:56
  • 10314

【数据库2】创建与管理数据库表

本节引言: 本节介绍的是数据库中表的创建与管理,另外,因为不同DBMS所 支持的数据类型有所差别,这里就不慢慢阐述了,有需要的自行百度~1.创建一个最简单的表 假如:我们定义一个名叫 T_Pe...
  • daochugundepidan
  • daochugundepidan
  • 2015年11月14日 20:20
  • 199
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:数据库表间关系管理原理(2)
举报原因:
原因补充:

(最多只允许输入30个字)