eventsource
eventsource copied to clipboard
Question about Aggregate Validation
I have a question about event-sourcing, how can we validate if the Aggregate can be persisted? Like the example that create a User, how to validate that user has already exists? To restore all the aggregate from event is to expensive. And just check usercreatedevent is also not enough, because user maybe change the username or be deleted. hope you can shed some light on it
Global validations are particularly difficult for event sourcing systems. If you listen to Greg Young's talk, he suggests that you don't really need to do global validation.
That said, we came across the same issue. The way we solved it is in the single package, github.com/altairsix/eventsource/singleton.
Commands that need to guarantee some uniqueness implement singleton.Interface. The singleton package uses a separate dynamodb table to manage the uniqueness of resources.
It could use better documentation, but the general pattern is:
- create dynamodb table (pre-requisite)
- create event source registry
- create singleton instance
- using singleton.Registry#Wrap to wrap the Dispatch function
One of the test cases in https://github.com/altairsix/eventsource/blob/master/singleton/singleton_test.go provides an example of how the singleton is used.
@savaki thanks for the reply, I will have a look at the example