• 重载的乐趣


    菜鸟日记

    一直感觉重载很舒服,但是这次是感受到它的冲击力了,想通的那刻爽的不得了。我这里头有两张对象表,两张分类表,相对应出现了四张关系表,一个关系类把他们都解决了,哈哈~这就是OO的魅力啊。

    public class Relationship
    {
        DataClassesDataContext db;
        public Relationship()
        {
            db = new DataClassesDataContext(Utils.cnn);
        }
     
        #region Add methods
     
        public bool Add(oUrl url, tCategory category)
        {
            db.tUrlInCategory.InsertOnSubmit(new tUrlInCategory { Url_id = url.ID, Category_id = category.id });
            db.SubmitChanges();
            return true;
        }
        public bool Add(oUrl url, tDomain domain)
        {
            db.tUrlInDomain.InsertOnSubmit(new tUrlInDomain { Url_id = url.ID, Domain_id = domain.id });
            db.SubmitChanges();
            return true;
        }
        public bool Add(oPwd pwd, tCategory category)
        {
            db.tPwdInCategory.InsertOnSubmit(new tPwdInCategory { Pwd_id = pwd.id, Category_id = category.id });
            db.SubmitChanges();
            return true;
        }
        public bool Add(oPwd pwd, tDomain domain)
        {
            db.tPwdInDomain.InsertOnSubmit(new tPwdInDomain { Pwd_id = pwd.id, Domain_id = domain.id });
            db.SubmitChanges();
            return true;
        }
        #endregion
     
        #region Get methods
     
        public tUrlInCategory Get(oUrl url, tCategory category)
        {
            return (from c in UrlInCategory(url) where c.Category_id == category.id select c).First();
        }
        public tUrlInDomain Get(oUrl url, tDomain domain)
        {
            return (from c in UrlInDomain(url) where c.Domain_id == domain.id && c.Url_id == url.ID select c).First();
        }
        public tPwdInCategory Get(oPwd pwd, tCategory category)
        {
            return (from pc in db.tPwdInCategory where pc.tPwd.id == pwd.id && category.id == pc.tCategory.id select pc).First();
        }
        public tPwdInDomain Get(oPwd pwd, tDomain domain)
        {
            return (from pd in db.tPwdInDomain where pd.Domain_id == domain.id && pd.Pwd_id == pwd.id select pd).First();
        }
     
        public List<tUrlInCategory> UrlInCategory(oUrl url)
        {
            return (from c in db.tUrlInCategory where c.Url_id == url.ID select c).ToList();
        }
        public List<tUrlInDomain> UrlInDomain(oUrl url)
        {
            return (from c in db.tUrlInDomain where c.Domain_id == url.ID select c).ToList();
        }
        public List<tPwdInCategory> PwdInCategory(oPwd pwd)
        {
            return (from p in db.tPwdInCategory where p.tPwd.id == pwd.id select p).ToList();
        }
        public List<tPwdInDomain> pwdInDomain(oPwd pwd)
        {
            return (from p in db.tPwdInDomain where p.Pwd_id == pwd.id select p).ToList();
        }
        #endregion
     
        #region Delete methods
     
        public bool Delete(oUrl url, tCategory category)
        {
            db.tUrlInCategory.DeleteOnSubmit((Get(url, category)));
            db.SubmitChanges();
            return true;
        }
        public bool Delete(oUrl url, tDomain domain)
        {
            db.tUrlInDomain.DeleteOnSubmit(Get(url, domain));
            db.SubmitChanges();
            return true;
        }
        public bool Delete(oPwd pwd, tCategory category)
        {
            db.tPwdInCategory.DeleteOnSubmit(Get(pwd, category));
            db.SubmitChanges();
            return true;
        }
        public bool Delete(oPwd pwd, tDomain domain)
        {
            db.tPwdInDomain.DeleteOnSubmit (Get(pwd, domain));
            db.SubmitChanges();
            return true;
        }
        #endregion
    }
    作者:KKcat
        
    个人博客:http://jinzhao.me/
        
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    7. 流程控制
    6. 工作区域与包
    5. Go函数
    4. Go常量
    面试题1
    数据库三范式
    触发器和存储过程
    面试题
    js 程序执行与顺序实现详解 ,来自网上
    基础的优化数据库查询,个人笔记
  • 原文地址:https://www.cnblogs.com/jinzhao/p/1354306.html
Copyright © 2020-2023  润新知