只读数据库视图如何适应存储库模式?

发布于 2024-12-03 23:48:17 字数 166 浏览 4 评论 0原文

示例:您的数据库有一个名为“CustomerOrdersOnHold”的 SQL 视图。此视图返回特定客户和订单数据字段的过滤组合。您需要从应用程序中的该视图获取数据。对此类视图的访问如何适应存储库模式?您会创建一个“CustomerOrdersOnHoldRepository”吗?像这样的只读视图是否被视为聚合根?

Example: Your database has a SQL view named "CustomerOrdersOnHold". This view returns a filtered mix of specific customer and order data fields. You need to fetch data from this view in your application. How does access to such a view fit into the repository pattern? Would you create a "CustomerOrdersOnHoldRepository"? Is a read-only view such as this considered an aggregate root?

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

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

发布评论

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

评论(3

尸血腥色 2024-12-10 23:48:17

我更喜欢将读取存储库分开,最好甚至将其名称更改为 Finder 或 Reader,存储库用于域使用而不是用于查询只读数据,您可以参考 这个文章解释了 Finder 分离表单存储库的用法。

我还建议将读取模型与写入模型架构分开 CQRSthere

这种架构允许您将读取模型与写入模型分开,甚至在数据存储和使用事件溯源。

对于中间解决方案,您可以利用一些 CQRS 概念,而无需通过仅将存储库与查找器分离来分离数据库的复杂性,请阅读此 post

获取此类解决方案的示例(使用相同的数据库,但分开查找者形成存储库)检查此示例

I would prefer separating the read repository, preferably even change its name to Finder or Reader, the repository is meant for Domain usage not for querying read-only data, you can refer to this article and this which explains the usage of Finder separated form repository.

I would recommend also the separating of read model from write model architecture CQRS and there

This architecture allows you to separate the read model from write model even in terms of data storage and the use of event sourcing.

For a middle solution you can utilize some CQRS concepts without the complexity of separating database by just separating repository from finders, read this post

for a sample of this type of solution (use the same database but separating finders form repositories) check this sample

流年已逝 2024-12-10 23:48:17

您的只读数据将被视为 DDD 世界中的值对象。

我通常将值对象的访问方法放置在现有存储库中,直到创建单独的存储库有意义为止。它类似于可能返回要在地址表单上使用的静态状态列表的方法:

IAddressRepository
{
  Address GetAddress(string addressID);

  List<string> GetStates(string country);
}

Your read-only data would be considered Value Objects in the DDD world.

I typically place access methods for value objects in existing repositories until such time that it makes sense to create a separate repository. It's similar to a method that might return a static list of states to be used on an address form:

IAddressRepository
{
  Address GetAddress(string addressID);

  List<string> GetStates(string country);
}
小清晰的声音 2024-12-10 23:48:17

我认为拥有一个像“CustomerOrdersOnHoldRepository”这样的单独的存储库是很好的。存储库的接口将反映对象是只读的这一事实(通过不定义 Save/Add/MakePersistent 方法)。

来自如何编写存储库

...但是还有另一种我很喜欢的策略:多个
存储库。在我们的订购示例中,我们没有理由可以拥有
两个存储库:AllOrders 和 SurchargedOrders。所有订单代表
包含系统中每个订单的列表 SurchargedOrders
代表它的一个子集。

我不会将返回的对象称为聚合根。聚合用于一致性、数据交换和生命周期。你的对象没有这些。看来它们也不能被归类为值对象(“特征或属性”)。它们只是独立的类。

I think that it is fine to have a separate repository like "CustomerOrdersOnHoldRepository". The interface of the repository will reflect the fact that the objects are readonly (by not defining Save/Add/MakePersistent method).

From How to write a repository:

... But there is another strategy that I quite like: multiple
Repositories. In our ordering example there is no reason we can have
two Repositories: AllOrders and SurchargedOrders. AllOrders represent
a list containing every single order in the system, SurchargedOrders
represents a subset of it.

I would not call returned object an Aggrgate Root. Aggregates are for consistency, data exchange and life cycles. Your objects don't have any of these. It seems that they also can not be classified as Value Objects ('characteristic or attribute'). They are just standalone classes.

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