public void SaveEvents(Guid id, IEnumerable<Event> events, int expectedVersion) { EventDescriptionCollection collection = GetEventDescriptionCollection(id); // PERHAPS: If it's an entirely new event stream, no use in querying the database //if (events.First().Version != -1) //{ // collection = GetEventDescriptionCollection(id); //} if (collection == null) { collection = new EventDescriptionCollection(id); } else if (collection.Items.Any() && collection.LatestVersion != expectedVersion) { throw new ConcurrencyException(); } foreach (var @event in events) { expectedVersion++; @event.Version = expectedVersion; collection.Add(id, @event); } PersistEventDescriptionCollection(collection); foreach (var @event in events) { _serviceBus.Publish(@event); } }
protected override void PersistEventDescriptionCollection(EventDescriptionCollection collection) { lock (_syncLock) { if (!_eventStore.ContainsKey(collection.Id)) { _eventStore.Add(collection.Id, collection); } } }
protected abstract void PersistEventDescriptionCollection(EventDescriptionCollection collection);
protected override void PersistEventDescriptionCollection(EventDescriptionCollection collection) { _repository.Put(collection); }