/// <summary> /// Saves the given <paramref name="events" /> to the store. /// </summary> /// <param name="events">A list of events</param> /// <param name="expectedVersion">The current version of the latest event for an aggregate</param> public void Save(IEnumerable <IDomainEvent> events, int expectedVersion) { var eventList = events.ToList(); var firstEvent = eventList.FirstOrDefault(); if (firstEvent == null) { throw new EventStoreSaveException(); } var aggregateId = firstEvent.AggregateId; var aggregateType = firstEvent.AggregateType; var newVersion = expectedVersion; using (new TimeMeasure(ms => _performanceMeasurementService.CountCommit(eventList.Count, ms))) { if (expectedVersion != -1) { var lastVersion = GetEventsByAggregateId(aggregateId).Max(e => e.Version); //TODO: Performance-Optimization: Why loading all events and then only using the last if (lastVersion != expectedVersion) { throw new ConcurrencyException(); } } foreach (var @event in eventList) { newVersion++; @event.Version = newVersion; } _persistentEventStore.Add(eventList); //Create snapshot _eventBus.Publish(new AggregateSavedEvent(aggregateId, aggregateType, newVersion)); } //Publish events eventList.ForEach(e => _eventBus.Publish(e)); }
/// <summary> /// Adds the given <paramref name="@event" /> to the internal storage /// </summary> /// <param name="event">The event</param> public void Add(IDomainEvent @event) { _documentStoreCache.Add(@event); _persistentEventStore.Add(@event); }