/// <summary> /// Creates or updates the snapshot for the given aggregate root. /// </summary> /// <param name="aggregateRoot">The aggregate root on which the snapshot is created or updated.</param> public override void CreateOrUpdateSnapshot(ISourcedAggregateRoot <TAggregateRootId> aggregateRoot) { var snapshot = aggregateRoot.CreateSnapshot(); var dataObj = _snapshotSerializer.CreateFromAggregateRoot(aggregateRoot); var insertOrUpdateData = new PropertyBag(dataObj); var key = new EventNumberSnapshotMappingKey(aggregateRoot.GetType().AssemblyQualifiedName, aggregateRoot.Id); if (this.HasSnapshot(aggregateRoot.GetType(), aggregateRoot.Id)) { var aggregateRootTypeName = aggregateRoot.GetType().AssemblyQualifiedName; var aggregateRootId = aggregateRoot.Id; ISpecification <SnapshotDataObject <TAggregateRootId> > spec = Specification <SnapshotDataObject <TAggregateRootId> > .Eval( p => p.AggregateRootType == aggregateRootTypeName && p.AggregateRootId.Equals(aggregateRootId)); this.SnapshotStorage.Update <SnapshotDataObject <TAggregateRootId> >(insertOrUpdateData, spec); this.Committed = false; if (_snapshotMapping.ContainsKey(key)) { _snapshotMapping[key] = snapshot; } else { _snapshotMapping.Add(key, snapshot); } } else { this.SnapshotStorage.Insert <SnapshotDataObject <TAggregateRootId> >(insertOrUpdateData); this.Committed = true; _snapshotMapping.Add(key, snapshot); } }
/// <summary> /// Commits the changes registered in the domain repository. /// </summary> protected override void DoCommit() { foreach (var aggregateRoot in this.SaveHash) { var snapshotDataObject = _snapshotSerializer.CreateFromAggregateRoot(aggregateRoot); var aggregateRootId = aggregateRoot.Id; var aggregateRootType = aggregateRoot.GetType().AssemblyQualifiedName; var spec = Specification <SnapshotDataObject <TAggregateRootId> > .Eval(p => p.AggregateRootId.Equals(aggregateRootId) && p.AggregateRootType == aggregateRootType); var firstMatch = this._storage.SelectFirstOnly(spec); if (firstMatch != null) { this._storage.Update(new PropertyBag(snapshotDataObject), spec); } else { this._storage.Insert <SnapshotDataObject <TAggregateRootId> >(new PropertyBag(snapshotDataObject)); } foreach (var evnt in aggregateRoot.UncommittedEvents) { this.EventBus.Publish(evnt); } } if (this.DistributedTransactionSupported) { using (var ts = new TransactionScope()) { this._storage.Commit(); this.EventBus.Commit(); ts.Complete(); } } else { this._storage.Commit(); this.EventBus.Commit(); } }