存储库设计模式

发布于 2024-10-19 22:16:20 字数 177 浏览 2 评论 0原文

我见过很多存储库模式的实现。具体有两种类型

  1. 它们公开可查询的通用存储库,并期望服务类中的lambda表达式从数据库获取数据。

  2. 根据业务需求编写从数据库获取数据的方法,并封装检索数据的逻辑(甚至是 lambda)。

哪一种是更好的方法?

I have seen many repository pattern implementations. Specifically of 2 types

  1. They expose the generic repository which is queryable and expects a lamba expression from service class to get data from database.

  2. Write methods to get the data from database based on business requirements and encapsulate the logic (even lambda) of retrieving the data.

Which one is a better approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情痴 2024-10-26 22:16:20

我真的更喜欢第二个。

即使我看过 .NET 世界顶级博主的文章,对我来说,可查询在存储库中也是邪恶的。

原因:

  1. 存储库就像对象的集合,但存储在定义其实现的任何位置。

  2. 存储库抽象了数据映射到对象的方式。数据存储可以是任何东西,但业务依赖存储库来检索、添加、更新或删除域对象。

  3. 对底层存储的任何读或写访问都必须由存储库本身 - 或任何其他底层 - 管理。

可查询破坏了大部分这些点和/或原因。

例如,如果可以在 IQueryable 上使用 LINQ 来实现,为什么要设计 GetProductByNameGetProductByCode

并且 Queryable 在 n 层场景中效果不佳,因为除了返回延迟集的层之外,您无法访问另一层中的数据库连接。

我不认为“可查询”的概念和存储库应该对任何软件设计都有好处,因为它告诉存储库是无用的。

可查询就像设计一个GetAll方法。存储库中的 GetAll 有什么意义?存储库将检索所有域对象,您将在业务中过滤它们。但是...等等...存储库不应该检索具有某些条件的域对象,是吗?

尽管我发现 queryable 与存储库不兼容,但设计和实现一些接受 lambda 表达式或任何委托以提供某种过滤器或行为的存储库方法对我来说是可以的。这不是延迟执行,而是委托。

I really prefer the second one.

Even I've seen articles from top bloggers on .NET world, queryables, for me, are evil in a repository.

Reasons:

  1. A repository is like a collection of objects but stored wherever its implementation has defined.

  2. A repository abstracts the way data is mapped to object. Data store could be whatever, but business relies on repository in order to retrieve, add, update or remove domain objects.

  3. Any read or write access to the underlying store must be managed by the repository itself - or any other underlying layer -.

Queryable destroys most of these points and/or reasons.

For example, why you would design a GetProductByName, GetProductByCode, if you can do it with LINQ on IQueryable?

And Queryable works bad in n-tier scenarios, since you won't be having access to the database connection in another tier than the one which returned a deferred set.

I don't think "queryable" concept and repository should be good for any software design, because it's telling that repository is useless.

Queryable is like designing a GetAll method. What's the point of a GetAll in a repository? Repository will be retrieving all domain objects and you'll be filtering them in business. But... Wait... Repository isn't supposed to retrieve domain objects with some criteria, is it?

Although I find queryable incompatible with repository, designing and implementing some repository method that accepts a lambda expression or any delegate in order to give some kind of filter or behavior, for me, is fine. This isn't deferred execution, it's delegation.

独孤求败 2024-10-26 22:16:20

我更喜欢第一个。

可查询版本更好,因为它需要更少的代码并且更灵活

您不必提前知道您要寻找什么。

  // instead of repository.FindByMinimumAge(18)
  customerList = repository.Find<Customer>(c => c.Age >= 18)

使用第二种方法,您的存储库接口必须包含一个方法 FindByMinimumAge(intminimumAge) (以及一个 methodFindByName 和一个方法....)

update

存储库界面如下所示。

public interface IRepository<T> where T : class
{
    IEnumerable<T> Find(Expression<Func<T, bool>> where);
    ...
}

这可以通过 NHibernate、Linq2Sql 或“Mock using an arraylist”来实现。

I prefer the first one.

The queryable version is better because it requires less code and is more flexible.

You don't have to know in advance what you are looking for.

  // instead of repository.FindByMinimumAge(18)
  customerList = repository.Find<Customer>(c => c.Age >= 18)

With the second approach your Repository-Interface must contain a Method FindByMinimumAge(int minimumAge) (and a methodFindByName and a method ....)

update

The repository interface looks like this.

public interface IRepository<T> where T : class
{
    IEnumerable<T> Find(Expression<Func<T, bool>> where);
    ...
}

This can be implemented ie by NHibernate, Linq2Sql or "Mock using an arraylist".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文