我對存儲庫模式和依賴注入非常陌生。幾乎所有的倉庫模式我已經遇到了某種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(?)
有什麼建議?
UPDATE:
我想重用IRepository接口。例如,在一個程序中,我使用EF4來實現GetAll()方法,而在另一個程序中,我使用標準的ADO.NET來調用上述示例中的存儲過程。
你的代碼看起來完全沒問題,根據repostitory模式。 –