Esempio n. 1
0
        public AggregateRoot RestoreFromSnapshot(Snapshot snapshot)
        {
            if (snapshot == null)
            {
                return(null);
            }

            var aggregateRootType = _aggregateRootTypeProvider.GetAggregateRootType(snapshot.AggregateRootName);
            var aggregateRoot     = _aggregateRootFactory.CreateAggregateRoot(aggregateRootType);

            if (!IsSnapshotable(aggregateRoot))
            {
                throw new InvalidOperationException(string.Format("聚合根({0})没有实现ISnapshotable接口或者实现了多余1个的ISnapshotable接口,不能将其从某个快照还原。", aggregateRoot.GetType().FullName));
            }

            if (GetSnapshotDataType(aggregateRoot) != snapshot.Payload.GetType())
            {
                throw new InvalidOperationException(string.Format("当前聚合根的快照类型({0})与要还原的快照类型({1})不符", GetSnapshotDataType(aggregateRoot), snapshot.Payload.GetType()));
            }

            aggregateRoot.InitializeFromSnapshot(snapshot);

            SnapshotterHelper.RestoreFromSnapshot(snapshot.Payload, aggregateRoot);

            return(aggregateRoot);
        }
Esempio n. 2
0
        public virtual Snapshot CreateSnapshot(AggregateRoot aggregateRoot)
        {
            if (aggregateRoot == null)
            {
                throw new ArgumentNullException("aggregateRoot");
            }

            if (!IsSnapshotable(aggregateRoot))
            {
                throw new InvalidOperationException(string.Format("聚合根({0})没有实现ISnapshotable接口或者实现了多余1个的ISnapshotable接口,不能对其创建快照。", aggregateRoot.GetType().FullName));
            }

            var snapshotDataType = GetSnapshotDataType(aggregateRoot);
            var snapshotData     = SnapshotterHelper.CreateSnapshot(snapshotDataType, aggregateRoot);

            return(new Snapshot(aggregateRoot.GetType(), aggregateRoot.UniqueId, aggregateRoot.GetOriginalVersion(), snapshotData, DateTime.Now));
        }