public void Save(IAggregateRoot aggregateRoot, int expectedVersion)
        {
            var domainEvents = aggregateRoot.UncommittedEvents as IDomainEvent[] ?? aggregateRoot.UncommittedEvents.ToArray();

            if (aggregateRoot.Snapshotted)
            {
                foreach (var snapshot in aggregateRoot.Snapshots)
                {
                    PersistSnapshot(SnapshotObject.FromSnapshot(snapshot));
                }
            }

            SaveEvents(aggregateRoot.Id, domainEvents, expectedVersion);

            Task.Factory.StartNew(() => PublishEvents(domainEvents));
        }
Example #2
0
        /// <summary>
        ///     从快照创建快照持久化对象
        /// </summary>
        /// <param name="snapshot">领域事件</param>
        /// <returns></returns>
        public static SnapshotObject FromSnapshot(ISnapshot snapshot)
        {
            var obj = new SnapshotObject
            {
                SourceId  = snapshot.SourceId,
                Timestamp = DateTime.UtcNow,
                Version   = snapshot.Version,
            };

            var serializer = App.Container.Resolve <IObjectSerializer <string> >();

            string typeName;

            obj.Data = serializer.Serialize(snapshot, out typeName);
            obj.Type = typeName;

            return(obj);
        }
 /// <summary>
 ///     持久化快照
 /// </summary>
 /// <param name="snapshotObject"></param>
 protected abstract void PersistSnapshot(SnapshotObject snapshotObject);
        public ISnapshot GetSnapshot(Guid sourceId)
        {
            var snapshotObj = LoadSnapshotObject(sourceId);

            return(null == snapshotObj ? null : SnapshotObject.ToSnapshot(snapshotObj));
        }
Example #5
0
        /// <summary>
        ///     从持久化数据加载快照
        /// </summary>
        /// <param name="obj">持久化数据</param>
        /// <returns></returns>
        public static ISnapshot ToSnapshot(SnapshotObject obj)
        {
            var serializer = App.Container.Resolve <IObjectSerializer <string> >();

            return((ISnapshot)serializer.Deserialize(obj.Data, obj.Type));
        }