/// <summary> /// Save the specified <paramref name="context"/> changes for the given aggregate. /// </summary> /// <param name="aggregate">The current aggregate version for which the context applies.</param> /// <param name="context">The command context containing the aggregate changes to be applied.</param> public SaveResult Save(Aggregate aggregate, CommandContext context) { Verify.NotNull(aggregate, nameof(aggregate)); Verify.NotNull(context, nameof(context)); var commit = CreateCommit(aggregate, context); try { eventStore.Save(commit); } catch (DuplicateCommitException) { Log.Warn("Duplicate commit: {0}", commit); } // NOTE: Apply commit directly to existing aggregate. By default, each call to `Get` returns a new `Aggregate` instance. // Should the caller hold on to a reference to `this` aggregate instance, it is their responsibility to gaurd against // modifications (via `aggregate.Copy()` call) if they require an unaltered instance of `aggregate`. ApplyCommitToAggregate(commit, aggregate); if (aggregate.Version > 0 && aggregate.Version % snapshotInterval == 0) { snapshotStore.Save(new Snapshot(aggregate.Id, aggregate.Version, aggregate)); } return(new SaveResult(aggregate, commit)); }
/// <summary> /// Appends a new commit to the event store. /// </summary> /// <param name="commit">The commit to append to the event store.</param> public void Save(Commit commit) { eventStore.Save(commit); if (commit.Version == 1) { statistics.IncrementInsertCount(); } else { statistics.IncrementUpdateCount(); } }