替代与CQRS多到多关系 [英] Alternatives to many-to-many relationships with CQRS

查看:148
本文介绍了替代与CQRS多到多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何经典的多对许多关系与CQRS / DDD模型?

How do we model classic many-to-many relationships with CQRS/DDD?

我知道,无论DDD和CQRS实现和解决方案,往往是特定领域的,所以它可能难以拿出一个笼统的回答了这个问题。

I know that both DDD and CQRS implementations and solutions tend to be domain-specific, so it may be difficult to come up with a general answer to this question.

然而,让我们假设有之间的图书作者的熟悉的关系。这是一个典型的多对一对多的关系。

However, let's assume we have the familiar relationship between Book and Author. This is a classic many-to-many relationship.

要我来说,这似乎是最自然不过的图书作者的是两个不同的实体每一个在自己所属的聚合根。因此,明确造型它们之间的许多一对多的关系是不是要走的路。

To me, it seems most natural that Book and Author are two different Entities that each belong in their own Aggregate Root. Thus, explicitly modeling the many-to-many relationship between them is not the way to go.

我们如何模型的AddBookCommand?我们希望能够为一本书添加到我们的图书馆,也莫名其妙地指出,特定的作者的写这个的图书的。我们如何模型(并持续)这样的关系?

How do we model an AddBookCommand? We want to be able to add a book to our library, and also somehow state that a particular Author wrote this Book. How do we model (and persist) such a relationship?

既不的图书的也没有的作者的似乎是不错的人选值对象 ...

Neither Book nor Author seem like good candidates for Value Objects...

推荐答案

假设都是聚集,副本,需要进书汇总任何作者的数据在添加新的书,以便任何后续命令有足够的作者资料的项目。现在,如果作者总量大概需要由作者所写的书的信息,那么它可以订阅到NewBook​​Added事件(从技术上讲,你可以发送RegisterAsAuthorOfBook命令作者总量为NewBook​​Added事件的结果)。我presume人们可以这样周围的其他方法建模为好,但我不是书作者域亲密。

Supposing that both are aggregates, copy whatever author data you need into the Book aggregate upon adding the new book so that any subsequent commands have enough author data to work with. Now if the Author aggregate needs information about the books written by the author, then it could "subscribe" to the NewBookAdded event (technically you could send a RegisterAsAuthorOfBook command to the Author aggregate as a result of the NewBookAdded event). I presume one could model this the other way around as well, but I'm not that intimate with the Book Author domain.

底线是,你真的不存储多到很多,因为他们没有形成规模。你必须开始思考他们的(集合)将邮件发送给对方。更大的问题是什么需要是一致的,在什么时间点它需要是一致的。难道我们关心的作者不会立即反映新的图书已添加,这是她的/他是笔者的事实呢?是否有作者希望关于他/她写的书(反之亦然)?

Bottom line is that you don't really store many-to-many because they don't scale. You have to start thinking of them (aggregates) as sending messages to each other. The bigger question is what needs to be consistent and at what point in time does it need to be consistent. Do we care that the Author does not instantaneously reflect the fact a new Book has been added, of which she/he's the author? Are there any invariants that Author wants to enforce with regard to the books he/she has written (and vice versa)?

另一件事是停下来是面向数据,更注重的行为。什么是图书和作者聚集的行为?这会告诉在该点需要什么数据以及应该如何被建模

Another thing is to stop being data oriented and more behavior oriented. What's the behavior of the Book and Author aggregate? This will tell what data is required at which point and how it should be modelled.

http://pastie.org/1220582 在图书总量第一刺伤。

http://pastie.org/1220582 for a first stab at the Book aggregate.

这篇关于替代与CQRS多到多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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