haf / Documently

Domain Driven Design with CQRS, Event Sourcing, MassTransit, RavenDB, RabbitMQ and C# and F#.
http://architecture.jayway.com
218 stars 55 forks source link

viewmodels stop process events without save method #27

Closed perikarion closed 12 years ago

perikarion commented 12 years ago

I've been struggling with this issue quite long. According to CommonDomain source code events are dispatched via ApplyEvent method in AggregateBase class. For example when CreateCustomerCommandHandler is called, then Created event is dispatched. But when you comment out repo.Save(client, CombGuid.Generate(), null); line in Consume method in the same handler, events are not dispatched. Should't dispatching of events be independent from repository.Save method and should't be performed inside Aggregat Root (Customer in this case) or am I missing something?

haf commented 12 years ago

The events are dispatched as a part of the event store pipeline.

  1. Receive CMD
  2. Retrieve AR, hydrate
  3. Call method with CMD-data on AR
  4. AR mutates the 'UncommittedEvents' collection, adds its freshly minted events.
  5. Handler calls repo.Save(ar, MessageId, CommitId)
  6. ES: a. authorize hook b. cache hook c. save hook, marks events as uncommitted d. dispatch hook <- events are published e. post-dispatch hook <- events marked as committed

You can't dispatch from within AR, because the dispatch needs to happen after ACK from DB, of them having been saved, or you are forced to use 2PC transports.

Hope this clarifies things. I'm closing this issue, unless you have further questions.

perikarion commented 12 years ago

Thanks thats it, point six is what I was missing.