[英]DDD and EventSourcing how to detect redundant events in design
We are developing our application with help of EventSourcing.我们正在 EventSourcing 的帮助下开发我们的应用程序。 Let's imagine we have an Aggregate "InvestorAccount" - holds information about Investor's portfolio (stocks).
假设我们有一个聚合的“InvestorAccount”——保存有关投资者投资组合(股票)的信息。
We have 2 options which events should we have:我们有 2 个选项,我们应该有哪些事件:
InvestorAccountCreated
event InvestorAccountCreated
事件SharesBought
event SharesBought
事件SharesSold
event SharesSold
事件or或者
SharesBought
event SharesBought
事件SharesSold
event SharesSold
事件We are not sure we need InvestorAccountCreated
event.我们不确定是否需要
InvestorAccountCreated
事件。 We can use just SharesBought
as a first initial event.我们可以只使用
SharesBought
作为第一个初始事件。 The idea is - when investor bought shares - we automatically create account for him.这个想法是——当投资者购买股票时——我们会自动为他创建账户。 Right now it's doesn't make sense to have empty Invetor's account (with out shares) and we don't see bebefits of having
InvestorAccountCreated
event, but may be we still should have it, because it will be useful in the future?现在拥有空的 Invetor 账户(没有股票)是没有意义的,我们看不到拥有
InvestorAccountCreated
事件的好处,但也许我们仍然应该拥有它,因为它在未来会有用?
Do we need to to have InvestorAccountCreated
?我们需要拥有
InvestorAccountCreated
吗? And why?为什么?
The short answer is (as nearly always in DDD): it depends.简短的回答是(几乎总是在 DDD 中):这取决于。
If there's no concept in the domain of an investor account which has never owned a share, then it's probably not a great idea to have an InvestorAccountCreated
event, because that makes the state of an investor account which has never owned a share derivable from persisted events and forces a runtime validation that no InvestorAccountCreated
event is issued without immediately (or even better atomically) issuing a SharesBought
event.如果在从未拥有过股份的投资者账户的领域中没有概念,那么拥有
InvestorAccountCreated
事件可能不是一个好主意,因为这使得从未拥有过股份的投资者账户的状态可以从持续事件中推导出来并强制执行运行时验证,即在没有立即(或者甚至更好地原子地)发出SharesBought
事件的情况下不会发出任何InvestorAccountCreated
事件。
That said, by not having an InvestorAccountCreated
event, you are making it more difficult if it turns out that, actually, the domain needs to model an investor account that hasn't bought a share.也就是说,如果没有
InvestorAccountCreated
事件,如果事实证明域需要对尚未购买股票的投资者账户进行建模,就会变得更加困难。 The event can be added, but you've given up the ability to guarantee that for every investor account there's exactly one InvestorAccountCreated
(and thus, for instance, you can't count the number of investor accounts by counting the InvestorAccountCreated
events).可以添加该事件,但您已经放弃了保证每个投资者账户都有一个
InvestorAccountCreated
的能力(因此,例如,您不能通过计算InvestorAccountCreated
事件来计算投资者账户的数量)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.