Exemplo n.º 1
0
        /// <summary>
        /// 尝试从快照获取聚合根
        /// </summary>
        private bool TryGetFromSnapshot(string aggregateRootId, Type aggregateRootType, out AggregateRoot aggregateRoot)
        {
            aggregateRoot = null;

            var snapshot = _snapshotStore.GetLastestSnapshot(aggregateRootId, aggregateRootType);

            if (snapshot != null && snapshot.IsValid())
            {
                AggregateRoot aggregateRootFromSnapshot = ObjectContainer.Resolve <ISnapshotter>().RestoreFromSnapshot(snapshot);
                if (aggregateRootFromSnapshot != null)
                {
                    if (aggregateRootFromSnapshot.UniqueId != aggregateRootId)
                    {
                        string message = string.Format("从快照还原出来的聚合根的Id({0})与所要求的Id({1})不符", aggregateRootFromSnapshot.UniqueId, aggregateRootId);
                        throw new EventSourcingException(message);
                    }
                    var committedEventsAfterSnapshot = _eventStore.GetEvents(aggregateRootId, aggregateRootType, snapshot.Version + 1, long.MaxValue);
                    aggregateRootFromSnapshot.ReplaySourcableEvents(committedEventsAfterSnapshot);
                    aggregateRoot = aggregateRootFromSnapshot;
                    return(true);
                }
            }

            return(false);
        }
        private bool TryGetFromSnapshot(string aggregateRootId, Type aggregateRootType, out IAggregateRoot aggregateRoot)
        {
            aggregateRoot = null;

            var snapshot = _snapshotStore.GetLastestSnapshot(aggregateRootId, aggregateRootType);

            if (snapshot == null)
            {
                return(false);
            }

            aggregateRoot = _snapshotter.RestoreFromSnapshot(snapshot);
            if (aggregateRoot == null)
            {
                return(false);
            }

            if (aggregateRoot.UniqueId != aggregateRootId)
            {
                throw new Exception(string.Format("Aggregate root restored from snapshot not valid as the aggregate root id not matched. Snapshot aggregate root id:{0}, expected aggregate root id:{1}", aggregateRoot.UniqueId, aggregateRootId));
            }

            var aggregateRootTypeCode     = _aggregateRootTypeCodeProvider.GetTypeCode(aggregateRootType);
            var eventStreamsAfterSnapshot = _eventStore.QueryAggregateEvents(aggregateRootId, aggregateRootTypeCode, snapshot.Version + 1, int.MaxValue);

            aggregateRoot.ReplayEvents(eventStreamsAfterSnapshot);
            return(true);
        }
Exemplo n.º 3
0
        /// <summary>Try to get an aggregate root from snapshot store.
        /// </summary>
        private bool TryGetFromSnapshot(string aggregateRootId, Type aggregateRootType, out AggregateRoot aggregateRoot)
        {
            aggregateRoot = null;

            var snapshot = _snapshotStore.GetLastestSnapshot(aggregateRootId, aggregateRootType);

            if (snapshot == null)
            {
                return(false);
            }

            var aggregateRootFromSnapshot = ObjectContainer.Resolve <ISnapshotter>().RestoreFromSnapshot(snapshot);

            if (aggregateRootFromSnapshot == null)
            {
                return(false);
            }

            if (aggregateRootFromSnapshot.UniqueId != aggregateRootId)
            {
                var message = string.Format("Aggregate root restored from snapshot not valid as the aggregate root id not matched. Snapshot aggregate root id:{0}, required aggregate root id:{1}", aggregateRootFromSnapshot.UniqueId, aggregateRootId);
                throw new Exception(message);
            }

            var eventsAfterSnapshot = _eventStore.Query(aggregateRootId, aggregateRootType, snapshot.Version + 1, long.MaxValue);

            aggregateRootFromSnapshot.ReplayEventStreams(eventsAfterSnapshot);
            aggregateRoot = aggregateRootFromSnapshot;
            return(true);
        }
Exemplo n.º 4
0
        private bool TryGetFromSnapshot <T>(string aggregateRootId, int aggregateRootType, out T aggregateRoot)
            where T : class, IEventSourced
        {
            var snapshot = _snapshotStore.GetLastestSnapshot(aggregateRootId, aggregateRootType).ContinueWith(task => {
                if (task.Status == TaskStatus.Faulted)
                {
                    _logger.Error(task.Exception, "get the latest aggregateRoot from snapshotStore failed. aggregateRootId:{0},aggregateRootType:{1}.", aggregateRootId, typeof(T).FullName);
                    return(null);
                }

                return(task.Result);
            }).Result;

            if (snapshot == null)
            {
                aggregateRoot = null;
                return(false);
            }

            aggregateRoot = _serializer.Deserialize <T>(snapshot.Data);

            return(true);
        }