Ejemplo n.º 1
0
        /// <summary>
        /// Attempts to get the aggregate root entity associated with the aggregate identifier.
        /// </summary>
        /// <param name="identifier">The aggregate identifier.</param>
        /// <param name="cancellationToken"></param>
        /// <returns>The found <typeparamref name="TAggregateRoot"/>, or empty if not found.</returns>
        public async Task <Optional <TAggregateRoot> > GetOptionalAsync(string identifier, CancellationToken cancellationToken)
        {
            if (_unitOfWork.TryGet(identifier, out var aggregate))
            {
                return(new Optional <TAggregateRoot>((TAggregateRoot)aggregate.Root));
            }

            var snapshot = await _eventStore.Advanced
                           .GetSnapshotAsync(identifier, Int32.MaxValue, cancellationToken)
                           .ConfigureAwait(false);

            if (snapshot != null)
            {
                using (var stream = await _eventStore
                                    .OpenStreamAsync(snapshot, Int32.MaxValue, cancellationToken)
                                    .ConfigureAwait(false))
                {
                    var root = _rootFactory();
                    root.RestoreSnapshot(snapshot.Payload);
                    root.Initialize(stream.CommittedEvents.Select(eventMessage => eventMessage.Body));
                    _unitOfWork.Attach(new Aggregate(identifier, stream.StreamRevision, root));
                    return(new Optional <TAggregateRoot>(root));
                }
            }

            using (var stream = await _eventStore
                                .OpenStreamAsync(identifier, cancellationToken, 0)
                                .ConfigureAwait(false))
            {
                if (stream.StreamRevision == 0)
                {
                    return(Optional <TAggregateRoot> .Empty);
                }

                var root = _rootFactory();
                root.Initialize(stream.CommittedEvents.Select(eventMessage => eventMessage.Body));
                _unitOfWork.Attach(new Aggregate(identifier, stream.StreamRevision, root));
                return(new Optional <TAggregateRoot>(root));
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Reads the stream indicated from the minimum revision specified up to the maximum revision specified or creates
 /// an empty stream if no commits are found and a minimum revision of zero is provided.
 /// </summary>
 /// <param name="storeEvents">The store events instance.</param>
 /// <param name="bucketId">The value which uniquely identifies bucket the stream belongs to.</param>
 /// <param name="streamId">The value which uniquely identifies the stream within the bucket to be created.</param>
 /// <param name="cancellationToken"></param>
 /// <param name="minRevision">The minimum revision of the stream to be read.</param>
 /// <param name="maxRevision">The maximum revision of the stream to be read.</param>
 /// <returns>A series of committed events represented as a stream.</returns>
 /// <exception cref="StorageException" />
 /// <exception cref="StorageUnavailableException" />
 /// <exception cref="StreamNotFoundException" />
 public static Task <IEventStream> OpenStreamAsync(this IStoreEvents storeEvents, string bucketId, Guid streamId, CancellationToken cancellationToken, int minRevision = int.MinValue, int maxRevision = int.MaxValue)
 {
     EnsureStoreEventsNotNull(storeEvents);
     return(storeEvents.OpenStreamAsync(bucketId, streamId.ToString(), minRevision, maxRevision, cancellationToken));
 }