Beispiel #1
0
        //https://eventstore.org/blog/20130306/getting-started-part-3-subscriptions/
        //SubscribeToAllFrom(this IEventStoreConnection target,
        //                    Position? lastCheckpoint,
        //                    CatchUpSubscriptionSettings settings,
        //                    Action<EventStoreCatchUpSubscription, ResolvedEvent> eventAppeared,
        //                    Action<EventStoreCatchUpSubscription> liveProcessingStarted = null,
        //                    Action<EventStoreCatchUpSubscription, SubscriptionDropReason, Exception> subscriptionDropped = null,
        //                    UserCredentials userCredentials = null)

        private Action <EventStoreCatchUpSubscription, ResolvedEvent> eventAppeared(Projection projection)
        => async(_, e) =>
        {
            // check system events and ignore them...
            if (e.OriginalEvent.EventType.StartsWith("$"))
            {
                return;
            }

            // find event type
            var eventType = eventTypeMapper.GetEventType(e.Event.EventType);

            if (eventType == null)
            {
                return;
            }
            // deserialize the event.
            var domainEvent = serializer.Deserialize(e.Event.Data, eventType);

            //build your projection
            await projection.Handle(domainEvent);

            //store current checkpoint
            checkpointStore.SetCheckpoint(e.OriginalPosition.Value, projection);

            Console.WriteLine($"{domainEvent} projected into {projection}");
        };
        public async Task <Snapshot> GetSnapshotAsync <T>(Type type, Guid aggregateId)
        {
            Snapshot snapshot = default(Snapshot);
            var      stream   = getStreamName(type, aggregateId.ToString());

            Console.WriteLine("getting snapshot stream name:" + stream);
            var streamEvents = await eventStoreConnection.ReadStreamEventsBackwardAsync(stream, StreamPosition.End, 1, false);

            Console.WriteLine("found events:" + streamEvents.Events.Length);
            if (streamEvents.Events.Any())
            {
                var result = streamEvents.Events.FirstOrDefault();

                var t = eventTypeMapper.GetEventType(result.Event.EventType);
                Console.WriteLine("Typeof" + t.FullName);
                Console.WriteLine("Typeof" + Encoding.ASCII.GetString(result.OriginalEvent.Data));
                snapshot = (Snapshot)serializer.Deserialize(result.OriginalEvent.Data, t);
                Console.WriteLine("build snapshot:" + snapshot.AggregateId);
                return(snapshot);
            }

            return(null);
        }