linq,網(wǎng)上已經(jīng)有很多討論這個(gè)東西的文章,說好的說不好的比比皆是。在這兒我只說自己的觀點(diǎn),我覺得linq的好處就是繼承了一種思想,那就是對所有不同格式數(shù)據(jù)源的統(tǒng)一訪問標(biāo)準(zhǔn)。一種技術(shù)出來總是很快會被新技術(shù)代替,只有思想會延續(xù)比較長的時(shí)間,甚至可能影響更新?lián)Q代。個(gè)人感覺linq就是一種很好的思想,不知道其他的像java這樣的框架在以后會不會借鑒這種思想,但是我預(yù)測在不久的將來我們一定能夠看到在非微軟的框架中看到這種思想的衍生物。我們在使用linq to sql 的時(shí)候,會發(fā)現(xiàn)對于每一個(gè)數(shù)據(jù)庫實(shí)體對象的增刪改查都要寫很多相類似的代碼。雖然這些代碼并不是很多,但是對于習(xí)慣于重用的我們來說,總是寫相似的代碼還是有些小不爽的。.net中提供了一個(gè)很好的工具就是泛化,用來實(shí)現(xiàn)算法級別的重用(區(qū)別于繼承是代碼級別的重用),在下面我就會用泛化來實(shí)現(xiàn)一個(gè)linq to sql 的模板類,使用這個(gè)模板,對于簡單的單實(shí)體對象的簡單的增刪改查是可以實(shí)現(xiàn)的。但是對于很復(fù)雜的linq操作,可以繼承自該類然后再實(shí)現(xiàn)自己的方法,不過考試#提示如果數(shù)據(jù)庫的操作相當(dāng)復(fù)雜的話,建議選用的數(shù)據(jù)庫訪問工具是ado.net 而不是 linq to sql。僅供探討。
下面來看具體的代碼:
public class Base where T:class
{
public DataClasses1DataContext db { get; set; }
public Base()
{
db = new DataClasses1DataContext();
}
///
/// generic method for insert
///
///DB Entity
/// the entity for insert
public void Insert(T entity)
{
db.GetTable(entity.GetType()).InsertOnSubmit(entity);
db.SubmitChanges();
}
///
/// generic method for query
///
/// DB Entity
///the result that return by query in DB using lamda
public IEnumerable Query(Func match)
{
return db.GetTable().Where(match);
}
///
/// generic method for delete
///
///DB Entity
/// the entity for delete
public void Delete(T entity)
{
db.GetTable(entity.GetType()).DeleteOnSubmit(entity);
db.SubmitChanges();
}
///
/// generic method for update
///
///DB Entity
/// the entity for updage
public void Update(T entity)
{
//the two line below looks like first delete and then insert
//the new entity in database is not really update
//but actually .net optimize them and translate them into
//an update sql expression
db.GetTable(entity.GetType()).DeleteOnSubmit(entity);
db.GetTable(entity.GetType()).InsertOnSubmit(entity);
db.SubmitChanges();
}
}
And then show how to use it:
Base b = new Base();
Education a = new Education
{
Name="gkfdddedddd",
};
b.Insert(a);
var es = b.Query(E => E.Name == "1234d9");
foreach (Education ee in es)
{
ee.Name = "12349";
b.Update(ee);
b.Delete(ee);
}
以上的代碼實(shí)現(xiàn)比較簡單,應(yīng)該不會造成理解上的問題,有興趣探討的(不僅僅是這些代碼,還可以是linq 相關(guān)的其他方面)。
下面來看具體的代碼:
public class Base
{
public DataClasses1DataContext db { get; set; }
public Base()
{
db = new DataClasses1DataContext();
}
///
/// generic method for insert
///
///
/// the entity for insert
public void Insert
{
db.GetTable(entity.GetType()).InsertOnSubmit(entity);
db.SubmitChanges();
}
///
/// generic method for query
///
/// DB Entity
///
public IEnumerable
{
return db.GetTable
}
///
/// generic method for delete
///
///
/// the entity for delete
public void Delete
{
db.GetTable(entity.GetType()).DeleteOnSubmit(entity);
db.SubmitChanges();
}
///
/// generic method for update
///
///
/// the entity for updage
public void Update
{
//the two line below looks like first delete and then insert
//the new entity in database is not really update
//but actually .net optimize them and translate them into
//an update sql expression
db.GetTable(entity.GetType()).DeleteOnSubmit(entity);
db.GetTable(entity.GetType()).InsertOnSubmit(entity);
db.SubmitChanges();
}
}
And then show how to use it:
Base
Education a = new Education
{
Name="gkfdddedddd",
};
b.Insert(a);
var es = b.Query(E => E.Name == "1234d9");
foreach (Education ee in es)
{
ee.Name = "12349";
b.Update(ee);
b.Delete(ee);
}
以上的代碼實(shí)現(xiàn)比較簡單,應(yīng)該不會造成理解上的問題,有興趣探討的(不僅僅是這些代碼,還可以是linq 相關(guān)的其他方面)。