将关系数据迁移到事件存储

发布于 2024-11-17 18:07:12 字数 499 浏览 5 评论 0原文

我正在使用我自己的 CQRS 模式实现将旧项目迁移到 CQRS。我开始这次迁移的主要原因是为了摆脱 n 层架构造成的混乱。然而,与许多其他项目一样,该项目正在使用关系数据存储。

此过程中的当前状态是我有进行更改的命令和隔离查询的查询对象。这意味着从客户端到服务器,我有一种类似 CQRS 的方式来更改和查询数据,尽管我没有发布更改中的任何事件,也没有非规范化的读取存储。我应该提到的是,我也有相当贫乏的 DTO 作为我的“域模型”。所有行为都分布在 n 层及其处理程序、管理器和所有那些可怕的层事物中。

我想知道我该如何采取下一步。我现在想要的是开始构建一个负责其行为的域模型,原因是我想开始使用事件存储作为事实来源,这让我想到了我的问题:

如何我将数据从关系数据存储迁移到事件存储中?

我有一个极其规范化的数据模型,其中包含非常重要的数据,必须进行迁移。我确实明白,我不能期望从该数据中捕获任何意图,因为它已经死了,但我应该用它做什么?我应该创建大量迁移命令吗?很高兴听到您的经验。

I am migrating an old project towards CQRS using my own implementation of the CQRS pattern. The main reason I started this migration was to get rid of the mess the n-tier architecture had caused. The project however, as many others, is using a relational data store.

The current state in this process is that I have Commands that make changes and Query objects that isolate the querying. This means that from the client to server, I have a CQRS-ish way of changing and querying data although I do not publish any events from the changes, nor do I have a denormalized read store. I should mention that I also have pretty anaemic DTOs as my "domain model". All behaviour was distributed throughout the n-tier layers with its handlers, managers and all those horrible layer things.

I want to know how I can take the next step. What I want now is to start building a domain model that is in charge of its behaviour and the reason for that is that I want to start using an event store as the source of truth which brings me to my question:

How can I migrate the data from the relation data store into the event store?

I have an extremely normalized data model with very important data, which has to be migrated. I do understand that I cannot expect to capture any intent from that data as it is dead, but what should I do with it? Should I create loads of migration commands? It would be nice to hear your experience from this.

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

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

发布评论

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

评论(2

带上头具痛哭 2024-11-24 18:07:12

我还没有机会尝试这个,但我想尝试的东西可以在多个阶段中发挥作用:

  1. 首先绘制出系统需要的所有事件。
  2. 接下来将视图引入关系数据库来表示事件。
  3. 编写一个自定义实用程序以从视图中进行选择以构建正确的事件对象。
  4. 让自定义实用程序按正确的顺序将事件对象写入事件存储。
  5. 最后确保从事件存储中刷新非规范化视图模型。

我认为事件写入事件存储的顺序有点棘手,需要一些思考。但鉴于这是一个迁移过程,只要域的最终状态正确,我认为这并不重要。如果您的关系表有时间戳,那么这可能会有所帮助。

我认为构建事件对象比创建命令更合适,因为关系数据代表过去发生的操作的域。

I’ve not had the opportunity to try this but something I wanted to try would work in a number of stages:

  1. First map out all the events that the system would need.
  2. Next introduce Views into the relational database to represent the events.
  3. Write a custom utility to select from the views to build the correct event objects.
  4. Have the custom utility write the event objects to the event store in correct sequence.
  5. Finally make sure to refresh denormalised view models from the event store.

I would imagine that the sequence in which the events are written to the event store is a bit tricky and would require a bit of thought. But seeing as this is a migration process then as long as the final state of the domain is correct I don’t think it matters to much. If your relational tables have timestamps then this could help.

I decided that building event objects was more appropriate rather that creating commands because the relational data represents the domain for actions that have happened in the past.

铜锣湾横着走 2024-11-24 18:07:12

我的处理方式与所提供的解决方案不同。我已经在两个不同的项目中完成了两次迁移。

让命令执行写入操作并使用查询执行读取操作的第一步是良好的第一步。

我要做的下一件事是从执行写入的相同命令引发事件。适应活动的举办。

这仅仅意味着在命令处理程序中只需构建事件并将它们推送到 EventStore 中。

在项目生命周期中越早开始提交事件,应用程序中的历史记录就越多。

现在,继续在命令处理程序中执行对规范化数据库的写入。

一旦您有几个引发事件的命令处理程序,下一步就非常容易了。构建一个事件处理程序/反规范化器,侦听引发的事件并简单地执行与命令处理程序完全相同的写入操作。

您现在所做的就是将 WRITE 职责移至非规范化器并远离命令处理程序。

这基本上就是我推荐的过程,正如我所说,我已经这样做了两次并且有效。

将数据库中的现有数据迁移到事件的问题有点棘手。您必须将该数据视为当前状态,因此您可以做的就是构建并提交(使用您编写的某些实用程序)为系统中的所有数据创建事件。

例如,要迁移现有的 Accounts 表,您需要编写一个 AccountCreatedEvent 并将其与表中每个帐户中的所有数据一起提交。

Id approach this differently to the solutions offered. Ive done this migration twice on 2 different projects.

Your first step of having commands do the writes and queries do the reads is a good first step.

The next thing I would do is raise events from the same commands that do the writes. Get comfortable with the event raising.

This simply means in the command handler just build the events and push them into the EventStore.

The earlier in the projects life you start committing events, the more history you have in your app.

Now, continue to perform the writes to the normalized database in your command handlers.

Once you have a couple of command handlers raising events, the next step is quite easy. Build an event handler/denormalizer that listens for the raised event and simply performs EXACTLY the same write operation the command handler did.

All you have done now is move the WRITE responsibility into the denormalizer and away from the command handler.

Thats basically the process I would recommend, as I said, Ive done this twice and it works.

The matter of migrating EXISTING data in your data base to events is a little tricky. You have to think of that data as the current state, so all you can do I build and commit (using a some utility you write) Create events for all the data in your systems.

Eg, to migrate an existing Accounts table, you'd write an AccountCreatedEvent and commit it with all the data in each account in the table.

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