数据库表间关系管理原理(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控件及  通用的分页解决方案 ,取得比较好的效果

相关文章推荐

EBS相关表及表间关系

  • 2012年10月26日 08:28
  • 1.11MB
  • 下载

SaltStack实战之配置管理-状态间关系

SaltStack实战之配置管理-状态间关系 1. 状态相关模块 1.1 pkg 1.2 file 1.3 service 1.4 requisites 2. 实际应用...
  • ygqygq2
  • ygqygq2
  • 2017年06月16日 09:00
  • 194

binder 2 用户空间数据结构间关系

数据结构的建立及之间的关系: 1.全局变量: hlist_head: binder_procs:   binder_proc通过node: hlist_node关联进binder_procs stru...

oracle三对内存参数间关系之5AMM启动和关闭两种情况下pga自动管理涉及的一对参数间的关系探讨

oracle三对内存参数间关系之4AMM启动和关闭两种情况下 pga_aggregate_target和workarea_size_policy参数间的关系 引言: 在AMM关闭时(即m...
  • haiross
  • haiross
  • 2014年12月09日 09:09
  • 491

Node.js ORM框架Sequlize之表间关系

Sequelize模型之间存在关联关系,这些关系代表了数据库中对应表之间的主/外键关系。基于模型关系可以实现关联表之间的连接查询、更新、删除等操作。本文将通过一个示例,介绍模型的定义,创建模型关联关系...
  • LeoJame
  • LeoJame
  • 2017年07月26日 15:15
  • 159

Entity Framework的表间关系实现篇(一)

ORM的一个大核心就是实现表间的关系,总结一下EF的实现方式: 1. 一对多的关系: 1.1 隐喻的方式 public class Lodging { … public Des...

MicroERP开发技术分享:vsFlexGrid、scriptControl实现工资表自定义列与表间关系计算

开发大型的MIS系统,肯定是离不开第三方控件的,同时也要根据项目需要自己写几个。 MicroERP共用了以下几个控件: 第三方商业控件: vsFlexGrid:大名鼎鼎的表格控件,不用多说,配合vs...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:数据库表间关系管理原理(2)
举报原因:
原因补充:

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