存储库模式:限制、目的和好处 [英] Repository pattern: Limitation, purpose , and benefits

查看:59
本文介绍了存储库模式:限制、目的和好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是存储库模式的新手,我提出了以下问题:

I am new to repository pattern, and I come up the following questions:

  1. 存储库模式的目的是什么?
  2. 存储库模式的限制是什么?
  3. 存储库模式的好处是什么?
  4. 以下方法是否应该在存储库模式中公开?

<小时>

public IQueryable<T> GetQuery(){
    return _objectSet;
}

public IEnumerable<T> GetAll(){
    return GetQuery().AsEnumerable();
}

public IEnumerable<T> Find(Expression<Func<T, bool>> predicate){
    return _objectSet.Where(predicate);
}

public T Single(Expression<Func<T, bool>> predicate){
    return _objectSet.Single(predicate); 
}

public T First(Expression<Func<T, bool>> predicate){
    return _objectSet.First(predicate);
}

有什么帮助吗?

推荐答案

1.存储库模式的目的是什么?

1.What is/are the purpose of repository pattern?

抽象出数据源以降低复杂性.通过这样做,调用者不必对数据源或它的怪癖有任何了解.他们只知道他们可以调用存储库并从任意数据源获取信息.

To abstract away the data source to reduce complexity. By doing so the caller do not have to have any knowledge about the data source or it's quirks. They just know that they can invoke the repository and get information from an arbitary data source.

2.存储库模式的限制是什么?

2.What is/are the limitation of repository pattern?

它仅用于抽象数据源.

一个软限制是人们发现很难理解如何设计存储库类.人们通常将存储库设为通用,这又会将 DB 实现泄漏给调用者,从而迫使调用者拥有特定于 DB 的知识(或 OR/M 知识).这些实现存在严重缺陷,与直接使用 OR/Ms 相比没有任何好处.

A soft limit is that people find it hard to understand how to design the repository classes. It's common that people make the repositories to generic which in turn leaks DB implementation to the invoker, thus forcing the caller to have DB specific knowlegde (or OR/M knowledge). Those implementations are seriously flawed and provide no benefit over using OR/Ms directly.

3.存储库模式的好处是什么?

3.What is/are the benefits of repository pattern?

不太复杂的代码.调用者 get 不那么复杂,因为他们只需要调用存储库中的方法来获取和存储数据源中的信息.并且数据访问层变得不那么复杂,因为存储库只专注于检索和存储信息.

Less complex code. The caller get's less complex since they only have to invoke a method in the repository to fetch and store information in the data source. And the Data Access Layer get's less complex sinvce the repositories only focuses on retreiving and storing information.

4.下面的方法是否应该在存储库模式中公开?

4.Do the methods below should be exposed in the repository pattern?

没有.IQueryable 泄漏数据库特定信息.例如,您需要知道 OR/M 句柄如何将 LINQ 转换为 IN SQL 子句,以及如何将 OR/M 配置为延迟加载/急切加载.

No. IQueryable<T> leaks DB specific information. You need for instance to know how the OR/M handles translates LINQ to IN SQL clauses and how the OR/M is configured for lazy/eager loading.

为了降低复杂性,存储库应该传达它支持的查询类型.使用 Expression>谓词 就像说向我扔任何东西,我会尽力处理它.不会很好用.再次.以IN 子句为例.它的 LINQ 语句在不同的 OR/Ms 之间有所不同.

To reduce complexity the repository should communicate what kind of queries it support. Using Expression<Func<T, bool>> predicate is like saying throw anything at me and I'll try to handle it. Won't work very well. Again. Take the IN clause as an example. The LINQ statement for it varies between different OR/Ms.

使用表达式还将构建正确查询的责任转移给调用者.这意味着调用者的单元测试还必须包括验证调用者是否进行了正确查询的测试.即更复杂的测试.

Using expressions also move the responsibility of constructing correct queries to the caller. That means that the unit tests for the caller also have to include tests for validating that the caller makes correct queries. i.e. more complex tests.

这篇关于存储库模式:限制、目的和好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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