private async Task <(TModel, ulong?)> ReadModelStateAsync(object id, TemporalOrder?temporalOrder, DateTime?temporalDateFrom, ulong?temporalNumberFrom)
        {
            if (temporalOrder != TemporalOrder.Newest && !temporalDateFrom.HasValue && !temporalNumberFrom.HasValue)
            {
                return(null, null);
            }

            var streamName = EventStoreCommon.GetStateStreamName <TModel>(id);

            TModel model            = null;
            ulong? modelEventNumber = null;

            long?count = null;

            if (temporalOrder == TemporalOrder.Newest)
            {
                count = 1;
            }

            if (temporalOrder == TemporalOrder.Newest || temporalDateFrom.HasValue || temporalNumberFrom.HasValue)
            {
                var eventData = (await Engine.ReadBackwardsAsync(streamName, null, count, temporalNumberFrom, null, temporalDateFrom)).LastOrDefault();
                if (eventData != null)
                {
                    var eventState = EventStoreCommon.Deserialize <EvenStoreStateData <TModel> >(eventData.Data.Span);
                    modelEventNumber = eventState.Number;
                    model            = eventState.Model;
                }
            }

            return(model, modelEventNumber);
        }
        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);
        }