简体   繁体   中英

DDD and EventSourcing how to detect redundant events in design

We are developing our application with help of EventSourcing. Let's imagine we have an Aggregate "InvestorAccount" - holds information about Investor's portfolio (stocks).

We have 2 options which events should we have:

  1. InvestorAccountCreated event
  2. SharesBought event
  3. SharesSold event

or

  1. SharesBought event
  2. SharesSold event

We are not sure we need InvestorAccountCreated event. We can use just SharesBought as a first initial event. 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?

Do we need to to have InvestorAccountCreated ? And why?

The short answer is (as nearly always in DDD): it depends.

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.

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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM