事件源和读取模型生成 [英] Event Sourcing and Read Model generation

查看:64
本文介绍了事件源和读取模型生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设堆栈溢出域问题和事件的以下定义:

Assuming Stack Overflow domain problem and the following definition of events:

UserRegistered(UserId, Name, Email)
UserNameChanged(UserId, Name)
QuestionAsked(UserId, QuestionId, Title, Question)

假设事件存储处于以下状态(按出现的顺序):

Assuming the following state of event store (in the order of appearance):

1) UserRegistered(1, "John", "john@gmail.com")
2) UserNameChanged(1, "SuperJohn")
3) UserNameChanged(1, "John007")
4) QuestionAsked(1, 1, "Help!", "Please!")

假定以下非规范化的读取模型用于列出问题(针对第一页)的SO):

Assuming the following denormalized read model for questions listing (for the first page of SO):

QuestionItem(UserId, QuestionId, QuestionTitle, Question, UserName)

以及以下事件处理程序(用于构建非规范化读取模型):

And the following event handler (which builds denormalized read model):

public class QuestionEventsHandler
{
    public void Handle(QuestionAsked question)
    {
        var item = new QuestionItem(
            question.UserId, 
            question.QuestionId, 
            question.Title, 
            question.Question, 
            ??? /* how should i get name of the user? */);
        ...
    }
}

我的问题是我如何找到提出问题的用户名?或更常见:如果我的非规范化读取模型需要特定事件中不存在的其他数据,我应该如何处理事件?

My question is how can i find the name of the user who asked a question? Or more common: how should i handle events if my denormalized read model requires additional data which is not exists in the particular event?

我已经检查了CQRS的现有样本,包括Greg Young的 SimpleSQRS Fohjin Mark Nijhof的样本。但是在我看来,它们仅对事件中包含的数据进行操作。

I've examined existing samples of CQRS including SimpleSQRS of Greg Young and Fohjin sample of Mark Nijhof. But it seems to me that they are operate only with data that is included in events.

推荐答案

只需使用所有

我记得,格雷格(Greg)的做法-在以这种方式创建和存储/发布事件的同时丰富事件。

Greg's approach, as I recall, - enrich the event while creating and store/publish it this way.

这篇关于事件源和读取模型生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆