Ejemplo n.º 1
0
    public async Task Save <T>(T aggregate, CommandMetadata metadata) where T : AggregateRoot
    {
        if (aggregate == null)
        {
            throw new ArgumentNullException(nameof(aggregate));
        }

        var streamName = GetStreamName(aggregate);

        var changes = aggregate.GetChanges().ToArray();

        await _store.AppendEvents(streamName, aggregate.Version, metadata, changes);

        // Append snapshot
        if (aggregate is AggregateRootSnapshot snapshotAggregate)
        {
            if ((snapshotAggregate.Version + changes.Length + 1) - snapshotAggregate.SnapshotVersion >= _snapshotThreshold)
            {
                await _store.AppendSnapshot(streamName, aggregate.Version + changes.Length, snapshotAggregate.GetSnapshot());
            }
        }

        aggregate.ClearChanges();
    }