public async Task <long> SaveAsync(AggregateBase aggregate, params KeyValuePair <string, string>[] extraHeaders) { var streamName = $"{aggregate.Identifier}-{aggregate.Id}"; var pendingEvents = aggregate.GetPendingEvents(); var originalVersion = (uint)aggregate.Version - (uint)pendingEvents.Count; //TODO: hamárvanilyenstream-akkorszálljonel-kezelés, StreamState enum ? try { IWriteResult result; var commitHeaders = CreateCommitHeaders(aggregate, extraHeaders); var eventsToSave = pendingEvents.Select(x => ToEventData(Guid.NewGuid(), x, commitHeaders)); result = await _eventStoreConnection.AppendToStreamAsync(streamName, originalVersion, eventsToSave); aggregate.ClearPendingEvents(); return(result.NextExpectedVersion); } catch (WrongExpectedVersionException ex) { throw new /*Concurrency*/ Exception("concurrency exception", ex); } }
private static IDictionary <string, string> CreateCommitHeaders(AggregateBase aggregate, KeyValuePair <string, string>[] extraHeaders) { var commitId = Guid.NewGuid(); var commitHeaders = new Dictionary <string, string> { { MetadataKeys.CommitIdHeader, commitId.ToString() }, { MetadataKeys.AggregateClrTypeHeader, aggregate.GetType().AssemblyQualifiedName }, { MetadataKeys.UserIdentityHeader, Thread.CurrentThread.Name }, // TODO - was Thread.CurrentPrincipal?.Identity?.Name { MetadataKeys.ServerNameHeader, "DefaultServerNameHEader" }, // TODO - was Environment.MachineName { MetadataKeys.ServerClockHeader, DateTime.UtcNow.ToString("o") } }; foreach (var extraHeader in extraHeaders) { commitHeaders[extraHeader.Key] = extraHeader.Value; } return(commitHeaders); }