private IEventStream PrepareStream(string bucketId, IAggregateEx aggregate, Dictionary <string, object> headers) { IEventStream stream; var streamsId = bucketId + "@" + aggregate.Id; if (!this._streams.TryGetValue(streamsId, out stream)) { this._streams[streamsId] = stream = this._eventStore.CreateStream(bucketId, aggregate.Id.AsString()); } foreach (var item in headers) { stream.UncommittedHeaders[item.Key] = item.Value; } aggregate.GetUncommittedEvents() .Cast <object>() .Select(x => new EventMessage { Body = x }) .ToList() .ForEach(stream.Add); return(stream); }
/// <summary> /// /// </summary> /// <param name="bucketId"></param> /// <param name="aggregate"></param> /// <param name="commitId"></param> /// <param name="updateHeaders"></param> /// <returns></returns> public Int32 Save(string bucketId, IAggregateEx aggregate, Guid commitId, Action <IDictionary <string, object> > updateHeaders) { if (_disposed) { throw new ObjectDisposedException("This instance of repository was already disposed"); } Dictionary <string, object> headers = PrepareHeaders(aggregate, updateHeaders); Int32 uncommittedEvents = aggregate.GetUncommittedEvents().Count; while (true) { IEventStream stream = this.PrepareStream(bucketId, aggregate, headers); int commitEventCount = stream.CommittedEvents.Count; try { stream.CommitChanges(commitId); aggregate.ClearUncommittedEvents(); return(uncommittedEvents); } catch (DuplicateCommitException dex) { stream.ClearChanges(); _logger.Debug(String.Format("Duplicate commit exception bucket {0} - id {1} - commitid {2}. \n{3}", bucketId, aggregate.Id, commitId, dex)); return(0); //no events commited } catch (ConcurrencyException e) { _logger.Warn(String.Format("Concurrency Exception bucket {0} - id {1} - commitid {2}. \n{3}", bucketId, aggregate.Id, commitId, e)); if (this.ThrowOnConflict(stream, commitEventCount)) { //@@FIX -> aggiungere prima del lancio dell'eccezione stream.ClearChanges(); throw new ConflictingCommandException(e.Message, e); } stream.ClearChanges(); } catch (StorageException e) { throw new PersistenceException(e.Message, e); } finally { ReleaseAggregateId(aggregate.Id); } } }