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

来源:互联网 发布:物业软件市场推广 编辑:程序博客网 时间:2024/06/05 10:13

这个是源码

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

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控件及  通用的分页解决方案 ,取得比较好的效果

原创粉丝点击