public async Task OnEvent(IAggregateEvent ev, string queryId = null)
        {
            queryId = queryId ?? ev.AggregateId;
            var query = await queryStore.Get(queryId);

            var data = query.Data;

            if (query.Version >= ev.SequenceId)
            {
                // event already applied
                return;
            }

            int originalVersion = query.Version;

            if (originalVersion < ev.SequenceId - 1)
            {
                // fastforward to the current event's version
                query = await FastForward(query, ev.SequenceId);
            }
            else
            {
                // query is sync, just apply the event
                query.Data    = mutator.InvokeEventMutator(data, ev);
                query.Version = ev.SequenceId;
                query.DateUtc = DateTime.UtcNow;
            }

            await queryStore.Set(queryId, query, originalVersion);
        }
        public async Task <ItemListQueryModel> List(Guid?checkCommandId)
        {
            var queryModel = await _queryStore.Get("ItemList");

            if (queryModel == null)
            {
                return(new ItemListQueryModel());
            }

            var hasCommandIdBeenProcessed = checkCommandId == null || await _queryStoreCommandHistory.Contains(checkCommandId.Value);

            var model = new ItemListQueryModel
            {
                LastUpdated    = queryModel.LastUpdated,
                Items          = JsonConvert.DeserializeObject <List <ItemQueryModel> >(queryModel.Data),
                AwaitingUpdate = !hasCommandIdBeenProcessed
            };

            return(model);
        }
Beispiel #3
0
        private async Task ShouldHandleEventsAndStoreQuery(string expectedStatsStr, Func <CounterQueryEventsHandler, IEnumerable <IAggregateEvent>, Task> handleChanges)
        {
            // arrange
            var expected = CreateStats(expectedStatsStr);
            var changes  = await MakeExpectedChanges(expected);

            // act
            var handler = new CounterQueryEventsHandler(this.queryStore, this.eventStore);

            await handleChanges(handler, changes);

            // assert
            await Task.Delay(100);

            var query = await queryStore.Get(AggregateId);

            if (query.Version != changes.Count())
            {
            }

            Assert.That(query.Version, Is.EqualTo(changes.Count()));
            AssertStats(query.Data, expected);
        }