想知道我是否需要使用Genericrepository模式和UnitOfWork来模拟存储库.我正在使用MOQ.Is它现在是多余的,因为我注意到EF 4.1有IDBSet.
我还没弄明白如何编写一些通用的IDBSet.如果你有一个实例IDBSet的例子,你能告诉我吗?
有什么建议?
unit-testing mocking unit-of-work repository-pattern entity-framework-4.1
我有一个具体的存储库实现,它返回实体的IQueryable:
public class Repository
{
private AppDbContext context;
public Repository()
{
context = new AppDbContext();
}
public IQueryable<Post> GetPosts()
{
return context.Posts;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我的服务层可以根据其他方法(其中,分页等)的需要执行LINQ
现在我的服务层设置为返回IEnumerable:
public IEnumerable<Post> GetPageOfPosts(int pageNumber, int pageSize)
{
Repository postRepo = new Repository();
var posts = (from p in postRepo.GetPosts() //this is IQueryable
orderby p.PostDate descending
select p)
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize);
return posts;
}
Run Code Online (Sandbox Code Playgroud)
这意味着在我的代码隐藏中,如果我想绑定到转发器或其他控件,我必须执行ToList().
这是处理返回类型的最佳方法,还是在从服务层方法返回之前需要转换为列表?