如何使用 CQRS 假装读取数据库中的重复值

发布于 2024-11-17 14:31:35 字数 646 浏览 3 评论 0原文

假设我的 ReadDatabase 中有一个 User 表(使用 SQL Server)。在常规读/写数据库中,我可以在表上放置索引,以确保不会将 2 个用户添加到具有相同电子邮件地址的表中。

因此,如果我尝试添加一个用户,其电子邮件地址已存在于我的表中的不同用户,则 sql 服务器将抛出异常。

在 Cqrs 中我不能这样做,因为如果我将 readdatabas 的写入与域模型分离,通过将其放在异步队列上,我将不会将异常抛回给我,并且我将向 UI 返回“OK”并用户会认为他已被添加到数据库中,但实际上他永远不会被添加到读取数据库中。

我可以在读取数据库中进行搜索,检查我的数据库中是否已存在具有电子邮件地址的用户,如果有,则通过异常返回到 UI。但是,如果他们同时按下“保存”按钮,我将对数据库进行两次检查,发现数据库中没有任何用户具有该电子邮件地址,我发回没问题。将其放入我的队列中,稍后它将失败(通过点击唯一标识符)。

我是否应该从我的 EventSource(它是 SQL Server)加载所有用户,然后检查该集合,看看我是否有一个用户已经拥有此电子邮件地址。这听起来也有点疯狂...

你们是怎么解决的?

我看到的方法是不使用异步队列,而是使用同步队列,但这会对性能产生非常严重的影响,特别是当您有许多“读取存储”要写入时...

这里需要一些帮助...

Say that I have a User table in my ReadDatabase (use SQL Server). In a regulare read/write database I can put like a index on the table to make sure that 2 users aren't addedd to the table with the same emailadress.

So if I try to add a user with a emailadress that already exist in my table for a diffrent user, the sql server will throw an exception back.

In Cqrs I can't do that since if I decouple the write to my readdatabas from the domain model, by puting it on an asyncronus queue I wont get the exception thrown back to me, and I will return "OK" to the UI and the user will think that he is added to the database, when infact he will never be added to the read database.

I can do a search in the read database checking if there is a user already in my database with the emailadress, and if there is one, then thru an exception back to the UI. But if they press the save button the same time, I will do 2 checks to the database and see that there isn't any user in the database with the emailadress, I send back that it's okay. Put it on my queue and later it will fail (by hitting the unique identifier).

Am I suppose to load all users from my EventSource (it's a SQL Server) and then do the check on that collection, to see if I have a User that already has this emailadress. That sounds a bit crazy too me...

How have you people solved it?

The way I can see is to not using an asyncronized queue, but use a syncronized one but that will affect perfomance really bad, specially when you have many "read storages" to write to...

Need some help here...

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

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

发布评论

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

评论(1

你与昨日 2024-11-24 14:31:35

搜索 CQRS Set Based Validation 将为您提供此问题的解决方案。

Greg Young 发布了有关采用最终一致性的业务影响的文章 http://codebetter.com/gregyoung/2010/08/12/eventual-consistency-and-set-validation/

Jérémie Chassaing 发布了关于发现域中缺失聚合根的文章 http ://thinkbeforecoding.com/post/2009/10/28/Uniqueness-validation-in-CQRS-Architecture

相关堆栈溢出问题:

如何在 CQRS 中处理基于集合的一致性验证?
CQRS 验证和验证独特性

Searching for CQRS Set Based Validation will give you solutions to this issue.

Greg Young posted about the business impact of embracing eventual consistency http://codebetter.com/gregyoung/2010/08/12/eventual-consistency-and-set-validation/

Jérémie Chassaing posted about discovering missing aggregate roots in the domain http://thinkbeforecoding.com/post/2009/10/28/Uniqueness-validation-in-CQRS-Architecture

Related stack overflow questions:

How to handle set based consistency validation in CQRS?
CQRS Validation & uniqueness

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