protected override sealed async Task PersistModelAsync(PersistEvent @event, TModel model, Graph <TModel> graph, bool create)
        {
            var id         = ModelAnalyzer.GetIdentity(model);
            var streamName = EventStoreCommon.GetStreamName <TModel>(id);

            var eventStoreModel = new EventStoreEventModelData <TModel>()
            {
                Source     = @event.Source,
                SourceType = @event.Source?.GetType().Name,
                Model      = model,
                Graph      = graph
            };

            var data = EventStoreCommon.Serialize(eventStoreModel);

            var eventNumber = await Engine.AppendAsync(@event.ID, @event.Name, streamName, null, create?EventStoreState.NotExisting : EventStoreState.Existing, data);

            if (eventNumber > 0 && eventNumber % SaveStateEvery == 0)
            {
                var thisEventData = (await Engine.ReadBackwardsAsync(streamName, eventNumber, 1, null, null, null))[0];

                var where = ModelAnalyzer.GetIdentityExpression <TModel>(id);

                var eventStates = await Repo.QueryAsync(new EventQueryMany <TModel>(thisEventData.Date, thisEventData.Date, where));

                var eventState = eventStates.Where(x => x.Number == eventNumber).Single();

                await SaveModelStateAsync(id, eventState.Model, eventState.Number);
            }
        }
Ejemplo n.º 2
0
        public async Task Append <TEvent>(TEvent @event, bool validateEventNumber = false) where TEvent : IEvent
        {
            var eventType = typeof(TEvent);
            var eventName = eventType.Name;

            await ApplyEvent(@event, eventType);

            var eventBytes = EventStoreCommon.Serialize(@event);

            await this.eventStore.AppendAsync(Guid.NewGuid(), eventName, streamName, validateEventNumber?LastEventNumber : null, validateEventNumber?(LastEventNumber.HasValue ? EventStoreState.NotExisting : EventStoreState.Existing) : EventStoreState.Any, eventBytes);

            await Bus.DispatchAsync(@event);
        }
        private void SaveModelState(object id, TModel model, ulong eventNumber)
        {
            var streamName = EventStoreCommon.GetStateStreamName <TModel>(id);

            var eventState = new EvenStoreStateData <TModel>()
            {
                Model   = model,
                Number  = eventNumber,
                Deleted = model == null
            };
            var data = EventStoreCommon.Serialize(eventState);

            Engine.Append(Guid.NewGuid(), "StoreState", streamName, null, EventStoreState.Any, data);
        }