Пример #1
0
        public async Task <TAggregate> RehydrateAsync(
            TAggregateId aggregateId,
            CancellationToken cancellationToken = default)
        {
            var connection = await _connectionWrapper.GetConnectionAsync(cancellationToken)
                             .ConfigureAwait(false);

            var streamName = GetStreamName(aggregateId);

            var events = new List <IDomainEvent <TAggregateId> >();

            StreamEventsSlice currentSlice;
            long nextSliceStart = StreamPosition.Start;

            do
            {
                currentSlice = await connection.ReadStreamEventsForwardAsync(
                    streamName,
                    nextSliceStart,
                    200,
                    false)
                               .ConfigureAwait(false);

                nextSliceStart = currentSlice.NextEventNumber;

                events.AddRange(currentSlice.Events.Select(Map));
            } while (!currentSlice.IsEndOfStream);

            var result = Aggregate <TAggregate, TAggregateId> .Create(
                events.OrderBy(
                    e => e.AggregateVersion));

            return(result);
        }
Пример #2
0
        /// <summary>Get a <see cref="PathState"/> for the given combination of parameters, creating one if necessary.</summary>
        /// <param name="game"></param>
        /// <param name="fileManager"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public PathState GetPathState(Game game, FileManager fileManager, string path)
        {
            if (game == null)
            {
                throw new ArgumentNullException("game");
            }
            if (fileManager == null)
            {
                throw new ArgumentNullException("fileManager");
            }

            var       id = Aggregate.Create(fileManager.Id, game, path);
            PathState state;

            path = path.Replace('\\', '/');
            if (!PathStates.TryGetValue(id, out state))
            {
                Type stateType = game.StateType;
                if (stateType == null)
                {
                    throw new ArgumentException(string.Format("The game {0} does not have an accepted {1} state type.", game, typeof(State).Name));
                }

                ConstructorInfo constructor = stateType.GetConstructor(new Type[] { typeof(AlexandriaManager), typeof(string), typeof(FileManager) });

                if (constructor == null)
                {
                    throw new Exception(string.Format("State type {0} does not have an appropriate constructor, like the one for {1}.", stateType.FullName, typeof(State).FullName));
                }

                state          = (PathState)constructor.Invoke(new object[] { this, path, fileManager });
                PathStates[id] = state;
            }

            return(state);
        }