菜鸟日记
一直感觉重载很舒服,但是这次是感受到它的冲击力了,想通的那刻爽的不得了。我这里头有两张对象表,两张分类表,相对应出现了四张关系表,一个关系类把他们都解决了,哈哈~这就是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
}