我有一个通用存储库模式,我现在看到我需要一个自定义方法来实现这个模式的一个特定实现,让我们调用实现 CustomerRepository 和方法 GetNextAvailableCustomerNumber。我有一些想法,但它们不符合面向对象设计的 SOLID 原则。
我首先考虑为该实现创建一个自定义存储库模式(ICustomerRepository),但这并不是很可行。经验告诉我,肯定有其他一些我目前还没有考虑甚至不知道的方法。此外,我认为为道路上的每一个颠簸发明一个新的存储库接口不应该如此轻松。
然后我考虑让 ICustomerRepository 继承 IRepository<Customer> 并为 GetNextAvailableCustomer 添加方法签名,但这非常违反 Liskov 的替换原则,我相信它也略微违反了单一责任模式。即使我只想使用 ICustomerRepository,我仍然可以实现基于 IRepository 的客户存储库。我最终会得到两种选择,并且客户端应该实现哪个接口不再是显而易见的。在这种情况下,我希望它只能实现 ICustomerRepository,而不是 IRepository<Customer>
解决这个问题的正确方法是什么?接口继承真的是要走的路,还是有其他理想的符合 LSP 的首选方法?
这是我的通用存储库接口:
public interface IRepository<T>
where T : IEntity
{
T GetById(int id);
IList<T> GetAll();
IEnumerable<T> Query(Func<T, bool> filter);
int Add(T entity);
void Remove(T entity);
void Update(T entity);
}