【发布时间】:2011-08-04 07:16:17
【问题描述】:
我对存储库模式和依赖注入非常陌生。我遇到的几乎所有存储库模式都有某种 GetAll() 方法,如下所示:
public interface IRepository<T>
{
IQueryable<T> GetAll();
// other CRUD methods here...
}
我在实现此接口和 GetAll() 方法时遇到问题,因为我正在调用一个存储过程,该过程需要根据用户输入更改的参数。我不想向存储库接口添加临时方法,例如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.
}
}
我也在使用依赖注入,所以我必须在绑定时向构造函数添加一些动态输入:
Bind<IRepository<Entity>>().To<ConcreteRepository>().WithConstructorArgument(?)
有什么建议吗?
更新:
我想重用 IRepository 接口。例如,在一个程序中,我使用 EF4 来实现 GetAll() 方法,而在另一个程序中,我使用标准 ADO.NET 来调用上面示例中的存储过程。
【问题讨论】:
-
您的代码看起来绝对没问题,并且符合存储库模式。
标签: c# stored-procedures dependency-injection repository-pattern