这个问题不是关于如何测试数据访问,也不是存储库。这是关于当我想在项目中使用我的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 <>但收集<> –