예제 #1
0
        public async Task <Nothing> AppendAsync(
            AggregateId aggregateId,
            IReadOnlyList <IDomainEvent> domainEvents,
            long expectedVersion)
        {
            if (domainEvents.Count > 0)
            {
                var connection = await EventStoreConnectionProvider.GrabSingleEventStoreConnectionFor(_connectionString);

                var results = await connection.ConditionalAppendToStreamAsync(
                    aggregateId.ToStreamName(_eventStoreName),
                    expectedVersion,
                    domainEvents.Select(e => e.ToEventData()));

                switch (results.Status)
                {
                case ConditionalWriteStatus.Succeeded:
                    break;

                case ConditionalWriteStatus.VersionMismatch:
                    throw new VersionMismatchException(aggregateId.ToStreamName(_eventStoreName), expectedVersion);

                case ConditionalWriteStatus.StreamDeleted:
                    throw new StreamDeletedException(aggregateId.ToStreamName(_eventStoreName));

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            return(Nothing.NotAtAll);
        }
예제 #2
0
        public async Task <IReadOnlyList <IDomainEvent> > AsyncLoadAllEventsFor(AggregateId aggregateId)
        {
            var connection = await EventStoreConnectionProvider.GrabSingleEventStoreConnectionFor(_connectionString);

            var resolvedEvents = await connection.ReadAllStreamEventsForward(aggregateId.ToStreamName(_eventStoreName));

            return(resolvedEvents.Select(e => e.Event.ToDomainEvent()).ToList());
        }
예제 #3
0
        private IEventStoreSubscription EventStoreSubscriptionFrom(string streamName, long lastCheckpoint)
        {
            var connection  = EventStoreConnectionProvider.GrabSingleEventStoreConnectionFor(_connectionString).Result;
            var eventStream = new BlockingCollection <IDomainEvent>();

            var catchUpSubscription = connection.SubscribeToStreamFrom(
                streamName,
                lastCheckpoint == 0 ? (long?)null : lastCheckpoint,
                CatchUpSubscriptionSettings.Default,
                (_, x) => eventStream.Add(x.Event.ToDomainEvent()));

            return(new EventStoreSubscription(catchUpSubscription, eventStream));
        }