public void Save(TAggregateRoot item) { var uncommittedEvents = item.GetUncommittedEvents().ToArray(); if (uncommittedEvents.Length == 0) { return; } if (_loaded.Contains(item.Id)) { object[] baseAndUnseenEvents = _eventStore.Load(item.Id).ToArray(); if (baseAndUnseenEvents.Length > 0) { var unseenEvents = GetUnseenEvents(item, baseAndUnseenEvents); var conflicts = _concurrencyMonitor.CheckForConflicts(unseenEvents, uncommittedEvents); if (conflicts.Any()) { var exception = new ConcurrencyConflictsDetectedException(conflicts); exception.Data.Add("AggregateId", item.Id); throw exception; } } _eventStore.Update(item.Id, uncommittedEvents); } else { _eventStore.Add(item.Id, uncommittedEvents); _loaded.Add(item.Id); } item.AcceptUncommittedEvents(); }
public void Save(TAggregateRoot item) { var uncommittedEvents = item.GetUncommittedEvents().ToArray(); if (uncommittedEvents.Length == 0) { return; } var streamId = EventStreamIdFormatter.GetStreamId <TAggregateRoot>(item.Id.ToString()); try { _eventStore.Save <TAggregateRoot>(streamId, item.BaseVersion, uncommittedEvents); } catch (EventStoreConcurrencyException) { EventStream <TAggregateRoot> stream = _eventStore.Load <TAggregateRoot>(streamId); if (stream != null) { IEvent[] baseAndUnseenEvents = stream.Events.ToArray(); if (baseAndUnseenEvents.Length > 0) { var unseenEvents = GetUnseenEvents(item, baseAndUnseenEvents); var conflicts = _concurrencyMonitor.CheckForConflicts(unseenEvents, uncommittedEvents); if (!conflicts.Any()) { // Re-version our uncommitted events on top of those already saved to the db. int unseenVersion = unseenEvents.Last().Version; int newVersion = unseenVersion; foreach (var evt in uncommittedEvents) { evt.Version = ++newVersion; } _eventStore.Save <TAggregateRoot>(streamId, unseenVersion, uncommittedEvents); } else { var exception = new ConcurrencyConflictsDetectedException(conflicts); exception.Data.Add("AggregateId", item.Id); throw exception; } } } } item.AcceptUncommittedEvents(); }
public void Save(TAggregateRoot item) { var uncommittedEvents = item.GetUncommittedEvents().ToArray(); if (uncommittedEvents.Length == 0) { return; } object[] baseAndUnseenEvents = _eventStore.Load(item.Id).ToArray(); if (baseAndUnseenEvents.Length > 0) { var unseenEvents = GetUnseenEvents(item, baseAndUnseenEvents); _concurrencyMonitor.CheckForConflicts(unseenEvents, uncommittedEvents); } _eventStore.Store(item.Id, uncommittedEvents); item.AcceptUncommittedEvents(); }