【发布时间】:2011-02-10 16:12:03
【问题描述】:
我只是想找出存储库模式的正确定义。
我最初的理解是这样的(非常愚蠢)
- 将业务对象与数据对象分开
- 标准化数据访问层的访问方法。
我确实见过 2 种不同的实现,网上也没有正式的例子,我看到的那些都藏在书里了。
实施 1:
public Interface IRepository<T>{
List<T> GetAll();
void Create(T p);
void Update(T p);
}
public interface IProductRepository: IRepository<Product> {
//Extension methods if needed
List<Product> GetProductsByCustomerID();
}
实施 2:
public interface IProductRepository {
List<Product> GetAllProducts();
void CreateProduct(Product p);
void UpdateProduct(Product p);
List<Product> GetProductsByCustomerID();
}
注意第一个是通用的 Get/Update/GetAll 等,第二个更像是我定义的“DAO”。
两者都共享从您的数据实体中提取的内容。我喜欢,但我可以用一个简单的 DAO 来做同样的事情。但是,我认为第二部分标准化访问操作很有价值,如果您在企业范围内实现此功能,人们将很容易知道您的存储库的访问方法集。
假设数据访问的标准化是这种模式的一个组成部分,我错了吗?如果两者都是正确的,为什么要选择执行 2?
Rhino有一篇关于实现1的好文章,当然MS有一个模糊的definition,实现2的一个例子是here。
【问题讨论】:
-
对我而言,接口是一种抽象,即与实现相反。我们在这里讨论的只是接口,还是实现类?
标签: c# design-patterns repository-pattern