コード例 #1
0
 public bool TryTakeSnapshot(Domain.AggregateRoot aggregateRoot, out Snapshot snapshot)
 {
     snapshot = null;
     try
     {
         var payload = ((dynamic)aggregateRoot).CreateSnapshot();
         snapshot = new Snapshot(aggregateRoot.EventSourceId, aggregateRoot.Version, payload);
         return(true);
     }
     catch (Exception ex)
     {
         Log.ErrorFormat("Cannot take snapshot for '{0}' aggregate. {1}",
                         aggregateRoot.GetType().FullName, ex.Message);
         snapshot = null;
         return(false);
     }
 }
コード例 #2
0
        public bool TryLoadFromSnapshot(Type aggregateRootType, Snapshot snapshot, CommittedEventStream committedEventStream, out Domain.AggregateRoot aggregateRoot)
        {
            aggregateRoot = null;

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

            if (AggregateSupportsSnapshot(aggregateRootType, snapshot.Payload.GetType()))
            {
                try
                {
                    Log.DebugFormat("Reconstructing aggregate root {0}[{1}] from snapshot", aggregateRootType.FullName,
                                    snapshot.EventSourceId.ToString("D"));
                    aggregateRoot = _aggregateRootCreator.CreateAggregateRoot(aggregateRootType);
                    aggregateRoot.InitializeFromSnapshot(snapshot);
                    aggregateRoot.RestoreFromSnapshot(snapshot.Payload);

                    Log.DebugFormat("Applying remaining historic event to reconstructed aggregate root {0}[{1}]",
                                    aggregateRootType.FullName, snapshot.EventSourceId.ToString("D"));
                    aggregateRoot.InitializeFromHistory(committedEventStream);
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Cannot load snapshot for '{0}' aggregate. {1}",
                                    aggregateRoot.GetType().FullName, ex.Message);
                    aggregateRoot = null;
                    return(false);
                }

                return(true);
            }

            return(false);
        }