示例#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);
        }
示例#2
0
        /// <summary>
        /// Apply event to the state and add to uncommited events
        /// </summary>
        /// <param name="evnt">the event</param>
        public void Apply(IEvent evnt)
        {
            if (string.IsNullOrEmpty(evnt.Id))
            {
                throw new NoAggregateIdException(string.Format("Event {0} has null (or empty) ID property. You may trying to update the aggregate that has not been created. Make sure you specify it on event creation", evnt.GetType().FullName));
            }

            StateSpooler.Spool(_state, evnt);
            _changes.Add(evnt);
        }