Linq to sql 存储库中的特定查询

Linq to sql 存储库中的特定查询,linq-to-sql,entity-framework-4,c#-3.0,repository-pattern,Linq To Sql,Entity Framework 4,C# 3.0,Repository Pattern,我已经读了很多关于存储库模式(Linq2SQL或EF)的书。我看到一些存储库带有一些特定的查询,例如FindUserByName,我将在其中向存储库传递一个where表达式 另一方面,我看到一些存储库只包含“Save、Update和GetAll”和GetAll返回IQueryable。并且,该IQueryable在服务层中被过滤 所以,在您看来,将特定的查询传递给存储库好吗?还是让它尽可能简单,让所有的过滤器都在服务中出现 谢谢 我的建议是创建一个通用存储库,它具有核心的基本方法(查找,保存,删

我已经读了很多关于存储库模式(Linq2SQL或EF)的书。我看到一些存储库带有一些特定的查询,例如
FindUserByName
,我将在其中向存储库传递一个where表达式

另一方面,我看到一些存储库只包含“Save、Update和GetAll”和
GetAll
返回IQueryable。并且,该IQueryable在服务层中被过滤

所以,在您看来,将特定的查询传递给存储库好吗?还是让它尽可能简单,让所有的过滤器都在服务中出现


谢谢

我的建议是创建一个
通用存储库
,它具有核心的基本方法(
查找
保存
删除
,等等)

示例:

public abstract class GenericRepository<T> : IRepository<T> where T : class
{
    public T FindSingle(Expression<Func<T,bool>> predicate) { .. };
    public IQueryable<T> Find() { .. };
    public void Delete(T entity) { .. };    
}
public class EmployeeRepository : GenericRepository<Employee>, IRepository<Employee>
{
   public Employee FindByLastName(string lastName)
   {
      return FindSingle(emp => emp.LastName == lastName);
   }

   public ICollection<Employee> FindAllManagers()
   {
      return Find().Where(emp => emp.Type == "Manager").ToList();
   }

   // other methods...
}
公共抽象类GenericRepository:IRepository,其中T:class
{
公共T FindSingle(表达式谓词){..};
公共IQueryable Find(){..};
公共无效删除(T实体){..};
}
然后创建从通用存储库继承的特定存储库,以创建专用方法

示例:

public abstract class GenericRepository<T> : IRepository<T> where T : class
{
    public T FindSingle(Expression<Func<T,bool>> predicate) { .. };
    public IQueryable<T> Find() { .. };
    public void Delete(T entity) { .. };    
}
public class EmployeeRepository : GenericRepository<Employee>, IRepository<Employee>
{
   public Employee FindByLastName(string lastName)
   {
      return FindSingle(emp => emp.LastName == lastName);
   }

   public ICollection<Employee> FindAllManagers()
   {
      return Find().Where(emp => emp.Type == "Manager").ToList();
   }

   // other methods...
}
公共类EmployeeRepository:GenericRepository,IRepository
{
公共雇员FindByLastName(字符串lastName)
{
返回FindSingle(emp=>emp.LastName==LastName);
}
公共ICollection FindAllManager()
{
返回Find().Where(emp=>emp.Type==“Manager”).ToList();
}
//其他方法。。。
}
这意味着您没有在存储库中复制公共代码

是的,另一种选择是使用
通用存储库
提供的服务。这意味着服务(本质上)是一个专门的存储库


因此,如果您想要一个服务层或专门的存储库,这只是一个偏好问题。

hmmm。。。我会想一想你说过的话!!谢谢