Repository模式实现 [英] Repository Pattern Implementation

查看:327
本文介绍了Repository模式实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来,我觉得库模式的每一个例子,实现在某种程度上是不同的。 。以下是两个例子我主要是找

It seems that every example I find of the repository pattern, the implementation is different in some way. The following are the two examples I mainly find.

interface IProductRepository
{
    IQueryable<Product> FindAll();
}



有那么通常其中谈到到存储库并调用的FindAll(另一层)方法,并执行如开头发现产品任何操作的字母s或获取产品中的特定类别。

There is then usually another layer which talks to the repository and calls the FindAll() method and performs any operations such as finding products beginning with the letter 's' or fetching products in a particular category.

另一个例子我找了很多把所有的查找方法到存储库

The other example I find a lot put all of the find methods into the repository

interface IProductRepository
{
    IEnumerable<Product> GetProductsInCategory(int categoryId);
    IEnumerable<Product> GetProductsStartingWith(string letter);
    IEnumerable<PromoCode> GetProductPromoCodes(int productId);
}



你推荐哪条路我走?或者有什么优势/彼此缺点是什么?

Which path do you recommend I take? Or what are the advantages/disadvantages from each other?

从看了我的理解的 http://martinfowler.com/eaaCatalog/repository.html 第一种方法似乎最能体现这一点?

From my understanding having read http://martinfowler.com/eaaCatalog/repository.html the first approach seems to best reflect this?

推荐答案

第一个是太可怕了。 的IQueryable 就像 GOD对象。这真的很难找到它的100%完整实现(即使是全部或/ MS)。您可以暴露你的ORM,而不是直接使用它,因为你可能会得到一个漏水的抽象层否则。

The first one is horrible. IQueryable is like a GOD object. It's really hard to find a 100% complete implementation of it (even among all OR/Ms). You can expose your ORM directly instead of using it since you'll probably get a leaky abstraction layer otherwise.

乔尔说,最好的(文本是从维基百科的文章 ):

Joel says it best (text is from the wikipedia article):

在斯波斯基的文章,他提请注意该工作的大部分时间抽象的例子很多,但其中底层的细节复杂性也不能忽视,因而驱使复杂到这应该由抽象本身

In Spolsky's article, he calls attention to many examples of abstractions that work most of the time, but where a detail of the underlying complexity cannot be ignored, and thus drives complexity into the software that was supposed to be simplified by the abstraction itself

Joels博客条目

第二种方法是多少更容易实现并保持抽象完好。

The second approach is much easier to implement and to keep the abstraction intact.

更新

您库违反了单一职责原则,因为它有两个原因改变。第一个是,如果产品的API被改变,另一种是,如果促销码的API被改变。你应该恕我直言,使用两种不同的存储库,如:

Your repository is violating Single Responsibility Principle since it got two reasons to change. The first is if the Products API is changed and the other is if the PromoCode API is changed. You should imho use two different repositories like:

interface IProductRepository
{
    IEnumerable<Product> FindForCategory(int categoryId);
    IEnumerable<Product> FindAllStartingWith(string letter);
}

interface IPromoCodeRepository
{
    IEnumerable<PromoCode> FindForProduct(int productId);
}



改变了一些事情:

Changed things:


  • 我倾向于首先查找时,返回的几个项目和获取如果单个项目被返回。

  • 更短的方法名=更容易阅读。

  • 单的责任。它更容易分不清什么使用该资料库的类有依赖关系。

  • I tend to begin methods with Find when several items are returned and Get if a single item is returned.
  • Shorter method names = easier to read.
  • Single responsibility. It's easier to tell what the classes that use the repositories have for dependencies.

小定义良好的接口,可以更容易地发现违反因为类破的原则往往会得到臃肿构造SOLID原则。

Small well defined interfaces makes it easier to spot violations of the SOLID principles since classes the break the principles tend to get bloated constructors.

这篇关于Repository模式实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆