Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IEventPublisher Question #13

Closed
penleychan opened this issue Jan 13, 2017 · 1 comment
Closed

IEventPublisher Question #13

penleychan opened this issue Jan 13, 2017 · 1 comment

Comments

@penleychan
Copy link

Hello, I am still learning event sourcing and trying examples out to get more deeper understanding.

Based on your Sample, I am confused why is Publish need to be called twice? On your Repository.cs

public void Save<T>(T aggregate, int? expectedVersion = null) where T : AggregateRoot
{
      if (expectedVersion != null && _eventStore.Get<T>(aggregate.Id, expectedVersion.Value).Any())
      {
            throw new ConcurrencyException(aggregate.Id);
      }

      var changes = aggregate.FlushUncommitedChanges();
       _eventStore.Save<T>(changes);

      if (_publisher != null)
      {
           foreach (var @event in changes)
           {
                _publisher.Publish(@event);
           }
      }
}

_eventStore.Save<T>(changes); Calls Publish from InMemoryEventStore class, then after that it calls Publish again on:

if (_publisher != null)
{
     foreach (var @event in changes)
     {
           _publisher.Publish(@event);
     }
}

Does InMemoryEventStore need Publish? Since Repository handles that?

@gautema
Copy link
Owner

gautema commented Jan 14, 2017

Hi.

The preferred way to do publishing is to let the EventStore do it. As you can see, the repository constructor with a publisher is marked obsolete, and should be avoided.

The reason to let the eventstore do the publishing is that in a real world scenario you would often send the events on a bus or save them in a different data store than the event store. You would then need distributed transactions to make sure events aren't saved and not published or the other way around. To avoid this problem, let the eventstore publish the events after they are written to disk.

@gautema gautema closed this as completed Jan 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants