Exemple #1
0
        public InMemoryStream AppendEvent(InMemoryEvent @event)
        {
            var list = Events.ToList();

            list.Add(@event);
            return(AppendEvents(list));
        }
Exemple #2
0
        private async Task <IExecutionResult> AppendToStreamInternalAsync(string streamId, long expectedVersion, params InMemoryEventData[] events)
        {
            long eventPosition = 1;

            if (expectedVersion == ExpectedVersion.NoStream || expectedVersion == ExpectedVersion.Any)
            {
                await CreateNewStream(streamId, streamId, events);

                eventPosition++;
            }
            else
            {
                var existingStream = await this.ReadStreamEventsForwardAsync(streamId, 0,
                                                                             int.MaxValue);

                existingStream.ThrowsIf(stream => !existingStream.HasValue, new AggregateNotFoundException(streamId))
                .ThrowsIf(stream => expectedVersion != stream.Value.Version,
                          new WrongExpectedStreamVersionException(expectedVersion.ToString(),
                                                                  existingStream.Value.Version.ToString()));

                expectedVersion = expectedVersion + events.Length;

                var newVersionedStream = existingStream.Value.ChangeVersion(expectedVersion);


                eventPosition = newVersionedStream.NextEventNumber;
            }

            foreach (var @event in events)
            {
                var            newEvent = InMemoryEvent.Create(@event.EventId, streamId, eventPosition, @event.EventType, @event, @event.EventMetadata, DateTime.Now);
                InMemoryStream existingStream;

                _inMemoryStore.TryGetValue(streamId, out existingStream);

                var updatedStream = existingStream.AppendEvents(new List <InMemoryEvent> {
                    newEvent
                });

                _inMemoryStore.TryUpdate(streamId, updatedStream, existingStream);

                eventPosition++;
            }

            return(ExecutionResult.Success);
        }