我对存储库模式和依赖项注入(inject)还很陌生。我遇到的几乎所有存储库模式都有某种 GetAll() 方法,如下所示:
public interface IRepository<T>
{
IQueryable<T> GetAll();
// other CRUD methods here...
}
我在实现此接口(interface)和 GetAll() 方法时遇到问题,因为我调用的存储过程需要一个根据用户输入而改变的参数。我不想向存储库接口(interface)添加临时方法,例如IQueryable<T> GetAll(string input);
.我也不想向构造函数添加参数,因为它对我来说看起来有点乱:
public class ConcreteRepository : IRepository<Entity>
{
string _storedProcedureInput;
public ConcreteRepository(string storedProcedureInput)
{
_storedProcedureInput = storedProcedureInput;
public IQueryable<Entity> GetAll()
{
// Call to stored procedure goes here passing in the
// _storedProcedureInput variable.
}
}
我也在使用依赖注入(inject),所以我必须在绑定(bind)时向构造函数添加一些动态输入:
Bind<IRepository<Entity>>().To<ConcreteRepository>().WithConstructorArgument(?)
有什么建议吗?
更新:
我想重用 IRepository 接口(interface)。例如,在一个程序中,我使用 EF4 实现 GetAll() 方法,而在另一个程序中,我使用标准 ADO.NET 调用存储过程,如上例。
最佳答案
听起来您的 GetAll 不一定获取全部。在这种情况下,您也可以重命名它或使用另一种方法来更准确地描述您的存储过程提供的功能,该方法采用可以传递给该过程的适当输入参数。
关于c# - 存储过程的存储库模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6937545/