private async Task AppendEvent(string stream, Event @event, long expectedVersion) { var eventData = _eventSerializer.SerializeEvent(@event); try { await _conn.AppendToStreamAsync(stream, expectedVersion, eventData); } catch (ES.Exceptions.WrongExpectedVersionException wrongVersionException) { throw new WrongExpectedVersionException($"Stream version does not match the expected version {expectedVersion}."); } }
public async Task AppendEventsToStream(string stream, List <Event> events) { var streamId = new StreamId(stream); var serializedEvents = new List <NewStreamMessage>(); foreach (var @event in events) { serializedEvents.Add(await _eventSerializer.SerializeEvent(@event)); } var store = await _streamStoreProvider.GetStreamStore(); await store.AppendToStream(streamId.Id, ExpectedVersion.Any, serializedEvents.ToArray()); }
private Dictionary <string, EventDto> MapEvents(string domainObjectId, IList <IEvent> events) { var items = new Dictionary <string, EventDto>(events.Count); for (var i = 0; i < events.Count; i++) { IEvent @event = events[i]; if (!domainObjectId.Equals(@event.Headers.AggregateId)) { throw new InvalidOperationException("Domainobject id did not match!"); } string content = serializer.SerializeEvent(@event); var dto = new EventDto { Body = content, Headers = @event.Headers.ToDictionary(), Id = @event.Headers.GetString(EventHeaderKeys.EventId) }; var bodyHash = eventHash.HashString(dto.Body); string header = string.Join('-', dto.Headers.Values); var headerHash = eventHash.HashString(header); dto.Headers.Add(EventHeaderKeys.BodyHash, bodyHash); dto.Headers.Add(EventHeaderKeys.HeaderHash, headerHash); dto.Headers.Remove(EventHeaderKeys.AggregateId); items.Add(i.ToString(), dto); } return(items); }
public EventStoreTests() { inMemoryCrudRepository = Substitute.ForPartsOf <InMemoryCrudRepository>(); eventStreams = new[] { new EventStream(Guid.NewGuid()), new EventStream(Guid.NewGuid()), new EventStream(Guid.NewGuid()) }; inMemoryCrudRepository.AttachRange(eventStreams); eventSerializer = Substitute.For <IEventSerializer>(); eventSerializer.SerializeEvent(null) .ReturnsForAnyArgs(ci => ("{\"bar\":" + ci.ArgAt <Event1>(0).Foo + "}", new VersionedTypeId("EventName", 5))); eventSerializer.DeserializeEvent(Arg.Any <string>(), new VersionedTypeId("EventName", 5)) .Returns(ci => new Event1((int)JObject.Parse(ci.ArgAt <string>(0))["bar"])); eventSerializer.SerializeEventMetadata(null) .ReturnsForAnyArgs(ci => JsonConvert.SerializeObject(ci.Arg <IReadOnlyDictionary <string, string> >() .Append(new KeyValuePair <string, string>("fakeSer", "true")) .ToDictionary(x => x.Key, x => x.Value))); eventSerializer.DeserializeEventMetadata(null) .ReturnsForAnyArgs(ci => { var json = JObject.Parse(ci.Arg <string>()); json["fakeDeser"] = "true"; return(new JsonMetadata(json)); }); FakeClock.Setup(); eventStreamRows = new[] { new EventStreamRow(Guid.NewGuid(), "{\"bar\":1}", "EventName", 5, eventStreams[0].Id, 1, Clock.Current.Now, "{\"doh\":\"1\"}"), new EventStreamRow(Guid.NewGuid(), "{\"bar\":2}", "EventName", 5, eventStreams[0].Id, 2, Clock.Current.Now, "{\"doh\":\"2\"}"), new EventStreamRow(Guid.NewGuid(), "{\"bar\":3}", "EventName", 5, eventStreams[1].Id, 3, Clock.Current.Now, "{\"doh\":\"3\"}"), new EventStreamRow(Guid.NewGuid(), "{\"bar\":4}", "EventName", 5, eventStreams[1].Id, 2, Clock.Current.Now, "{\"doh\":\"4\"}"), new EventStreamRow(Guid.NewGuid(), "{\"bar\":5}", "EventName", 5, eventStreams[1].Id, 4, Clock.Current.Now, "{\"doh\":\"5\"}"), new EventStreamRow(Guid.NewGuid(), "{\"bar\":6}", "EventName", 5, eventStreams[1].Id, 5, Clock.Current.Now, "{\"doh\":\"6\"}") }; expectedStoreRecords = eventStreamRows.Select((x, i) => new FakeEventStoreRecord() { Event = eventSerializer.DeserializeEvent(x.EventJson, new VersionedTypeId(x.EventName, x.EventVersion)), AdditionalMetadata = new Dictionary <string, string>() { { "doh", (i + 1).ToString() }, { "fakeDeser", "true" } }, EventId = eventStreamRows[i].Id, StreamSequenceNumber = eventStreamRows[i].StreamSequenceNumber }) .ToArray(); inMemoryCrudRepository.AttachRange(eventStreamRows); sut = new EventStore(inMemoryCrudRepository, eventSerializer); }
private ExternalEventRecord CreateExternalEventRecord(IEventMessage eventMessage) { Guid eventId = eventMessage.Metadata.GetEventId() ?? throw new InvalidOperationException($"Cannot store an external event ({eventMessage}) without ID"); (string eventJson, VersionedTypeId typeId) = eventSerializer.SerializeEvent(eventMessage.Event); string metadataJson = eventSerializer.SerializeEventMetadata(eventMessage.Metadata); return(new ExternalEventRecord(eventId, eventJson, typeId.Name, typeId.Version, metadataJson)); }
public EventData ToEventData(IEvent @event) { Precondition.For(@event, nameof(@event)).NotNull(); byte[] headers = eventEncoding.GetBytes(serializer.SerializeHeader(@event.Headers)); byte[] content = eventEncoding.GetBytes(serializer.SerializeEvent(@event)); string typeName = @event.Headers.GetString(EventHeaderKeys.EventType); return(new EventData(@event.Headers.EventId, typeName, true, content, headers)); }
public async Task AppendAsync(StreamId streamId, Event @event, CancellationToken cancellationToken) { var json = _eventSerializer.SerializeEvent(@event.Type, @event); var message = new NewStreamMessage(Guid.NewGuid(), @event.Type, json); await _streamStore.AppendToStream(streamId, @event.ExpectedVersion == 0?ExpectedVersion.Any : @event.ExpectedVersion, message, cancellationToken); }