DDD:尝试使用 C# 编写与 Poco、Repository、DTO 和 DAO 相关的排序和过滤代码?

发布于 2024-08-25 07:22:18 字数 486 浏览 4 评论 0原文

我从我的存储库中获取项目列表。现在我需要对它们进行排序和过滤,我相信这将在存储库中完成以提高效率。我认为有两种方法可以用 DDD 方式做到这一点:

  1. 将一个过滤器和一个充满条件的排序对象发送到存储库(这叫什么)?
  2. 存储库结果会生成一个具有 .filter 和 .sort 方法的对象? (这不会是 POJO/POCO,因为它包含多个对象?)。

那么答案是1、2还是其他呢?你能解释一下为什么吗?我倾向于#1,因为存储库只能发送我想要的数据(或者#2 能够像 LazyList 一样延迟访问数据吗?)代码示例(或网站链接)将非常有帮助。

示例:

  1. 产品 产品 = repo.GetProducts(mySortObject, myFilterObject); // Poco 产品列表
  2. .AddFilter("price", "lessThan", "3.99");产品.AddSort("价格", "降序");

I get a list of items from my Repository. Now I need to sort and filter them, which I believe would be done in the Repository for efficiency. I think there would be two ways of doing this in a DDD way:

  1. Send a filter and a sort object full of conditions to the Repository (What is this called)?
  2. Repository result would produce an object with .filter and .sort methods? (This wouldn't be a POJO/POCO because it contains more than one object?).

So is the answer 1, 2, or other? Could you explain why? I am leaning toward #1 because the Repository would be able to only send the data I want (or will #2 be able to delay accessing data like a LazyList?) A code example (or website link) would be very helpful.

Example:

  1. Product product = repo.GetProducts(mySortObject, myFilterObject); // List of Poco
  2. product.AddFilter("price", "lessThan", "3.99"); product.AddSort("price", "descending");

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

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

发布评论

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

评论(2

卸妝后依然美 2024-09-01 07:22:18

我个人会选择你的第一个选择。

如果您从 DDD 角度考虑第二个选项,那么产品对象(我假设是您的域对象)所了解的知识并不是您要解决的业务问题的真正一部分(即,您的域)。相反,排序和过滤用于用户界面或某些其他后端处理组件中。

此外,当从单一职责(又名 SOLID)角度查看第二个选项时,您会发现您的 Product 业务对象负责排序和过滤,这与产品完全无关。产品。

我就是这样看待事情的。我会对其他人的意见感兴趣。

I would personally go with your first option.

If you think about the second option from a DDD perspective the product object, which I'm assuming is your domain object, has a knowledge about something that isn't really part of the business problem you are trying to solve (I.E., you domain). Rather sorting and filtering is used in the user interface or some other back end processing component.

Additionally when looking at the second option from a Single Responsibility (aka SOLID) perspective you'll see that your Product business object has the responsibility for sorting and filtering, something that isn't at all related to a product.

That's how I see things. I would be interested in others opinions.

太阳公公是暖光 2024-09-01 07:22:18

这不是一个完整的答案,但您可能想了解一下 CQRS(命令查询职责分离)背后的一些想法(可以找到一些很好的链接 此处)。

CQRS 是一种思考 DDD 的方式,可以帮助澄清其中的一些内容。它的级别比您的具体问题更高,但可能会有所帮助。

从本质上讲,我认为这只会帮助您决定采用第一个选择(这就是我在类似情况下最终得到的结果)。我们将其称为查询对象。

This isn't a complete answer, but you might want to take a look at some of the thinking behind CQRS (Command Query Responsibility Segregation) (some good links can be found here).

CQRS is a way of thinking about DDD that can help clarify some of this. It's at a higher level than your specific question, but it might help.

Essentially I think it will just help you decide to go with your first option (which is what I ended up with in a similar situation). We called it a Query Object.

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