這個問題不是關於如何測試數據訪問,也不是存儲庫。這是關於當我想在項目中使用我的POCO類(我使用TDD製作的)時遇到的問題。 我的意思是,這是因爲Linq to Entities的限制(或者可能是我關於類的設計的問題)的問題。TDD和CodeFirst(實體框架)
我已經通過TDD抵達的類可以在接下來的(非常simplificated,當然):
public class Person{
public int IdPerson{ get; set; }
public string Name { get; set; }
public int Average { get; set; }
}
public class Course{
public int IdCourse { get; set;}
public Person Professor { get; set;}
public IEnumerable<Person> Staff { get; set;}
public IEnumerable<Person> Students { get; set;}
public IEnumerable<Person> GetGreatStudents(){
return Students.Where(n => n.Average > 8);
}
}
很簡單,不是嗎?
那麼......那麼問題是什麼?讓我們來看看......當我想從數據庫中檢索課程中的所有數據時(課程類在現實模型中有很多子集)對它們應用過濾器,唯一的辦法就是像這樣:
var course = from obj in Courses //Doesn't matter how i get the courses' collection
select {
IdCourse = obj.IdCourse,
GreatStudents = obj.GetGreatStudents(), // Here is the problem
OtherCollection = obj.OtherCollection.Where(n => n.Active).Select(n => n.Property1)
}
如你所知,它會返回一個錯誤,因爲GetGreatStudents的()功能:
LINQ到實體無法識別方法[...]這種方法 不能翻譯成商店表達。
我的意思是,我知道我可以直接與。凡替換方法(N => n.Average> 8)但它是可怕(和在現實中有很多的過濾器),並且就此而言,如果我不能使用我已經構建和測試的API,那麼TDD有什麼意義?
所以,我的問題是爲那些有TDD和實體框架開發經驗的人,如何在使用Linq to Entities時使用通過TDD設計的漂亮API?
你讀過這篇文章:http://stackoverflow.com/questions/2876616/returning-ienumerablet-vs-iqueryablet – Colin
我不跟着你@科林,這對我有幫助嗎? –
供參考:在真正的課堂上,我沒有IEnumerable <>但收集<> –