Esempio n. 1
0
        /// <summary>
        /// Fetches all events from the AR's stream and re-hydrates the AR
        /// using all events
        /// </summary>
        /// <param name="id">AR id</param>
        /// <returns>AR instance</returns>
        public TAggregateRoot Get(string id)
        {
            var streamId = id;

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(string.Format(
                                                "Aggregate ID was not specified when trying to get by id {0} aggregate", typeof(TAggregateRoot).FullName));
            }

            var snapshot           = _snapshots.LoadAsync(streamId).Result;
            var aggregateStateType = GetAggregateStateType(typeof(TAggregateRoot));

            object       state;
            EventsStream stream;

            if (snapshot == null)
            {
                state  = CreateAggregateState(aggregateStateType);
                stream = _eventSource.GetEventsStream(streamId);
            }
            else
            {
                state  = _serializer.Deserialize(snapshot.Payload, aggregateStateType);
                stream = _eventSource.GetEventsStream(streamId, snapshot.StreamVersion + 1);
            }

            var aggregate = new TAggregateRoot();

            StateSpooler.Spool(state ?? aggregate, stream.Events);
            aggregate.Setup(state, stream.End);

            return(aggregate);
        }