Example #1
0
        protected async Task Handler(IAllStreamSubscription sub, StreamMessage re, CancellationToken cancellationToken)
        {
            _debugLog?.Invoke("Subscription {Subscription} got an event {@Event}", sub.Name, re);
            _lastProcessedPosition = GetPosition(re);

            if (re.Type.StartsWith("$"))
            {
                await Store();
            }

            try {
                var evt = _eventSerializer.Deserialize(re.GetJsonData(cancellationToken).Result, re.Type);

                if (evt != null)
                {
                    _debugLog?.Invoke("Handling event {Event}", evt);

                    await Task.WhenAll(
                        _projections.Select(x => x.HandleEvent(evt, re.Position))
                        );
                }
            }
            catch (Exception e) {
                _log.LogWarning(e, "Error when handling the event {Event}", re.Type);
            }

            await Store();

            Task Store() => StoreCheckpoint(GetPosition(re), cancellationToken);
        }
Example #2
0
        public async Task <T> Load <T>(string id) where T : Aggregate, new()
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            var stream    = StreamName.For <T>(id);
            var aggregate = new T();

            try {
                await _eventStore.ReadStream(stream, StreamReadPosition.Start, Fold);
            }
            catch (Exceptions.StreamNotFound e) {
                throw new Exceptions.AggregateNotFound <T>(id, e);
            }

            return(aggregate);

            void Fold(JsonStreamEvent streamEvent)
            {
                var evt = Deserialize(streamEvent);

                if (evt == null)
                {
                    return;
                }

                aggregate !.Fold(evt);
            }

            object?Deserialize(JsonStreamEvent streamEvent)
            => _serializer.Deserialize(streamEvent.Data, streamEvent.EventType);
        }
Example #3
0
 public T Data <T>() where T : class => _serializer.Deserialize <T>(_document.Data);