Ejemplo n.º 1
0
 public static IEventHandlerBuilder <IRoadNetworkCommandQueue, TEvent> UseRoadNetworkCommandQueue <TEvent>(
     this IEventHandlerBuilder <TEvent> builder, IStreamStore store)
 {
     if (store == null)
     {
         throw new ArgumentNullException(nameof(store));
     }
     return(builder.Pipe <IRoadNetworkCommandQueue>(next => (message, ct) => next(new RoadNetworkCommandQueue(store), message, ct)));
 }
Ejemplo n.º 2
0
        public static IEventHandlerBuilder <IRoadRegistryContext, TCommand> UseRoadRegistryContext <TCommand>(
            this IEventHandlerBuilder <TCommand> builder, IStreamStore store, IRoadNetworkSnapshotReader snapshotReader, EventEnricher enricher)
        {
            if (store == null)
            {
                throw new ArgumentNullException(nameof(store));
            }
            if (enricher == null)
            {
                throw new ArgumentNullException(nameof(enricher));
            }
            return(builder.Pipe <IRoadRegistryContext>(next => async(message, ct) =>
            {
                var map = new EventSourcedEntityMap();
                var context = new RoadRegistryContext(map, store, snapshotReader, SerializerSettings, EventMapping);

                await next(context, message, ct);

                foreach (var entry in map.Entries)
                {
                    var events = entry.Entity.TakeEvents();
                    if (events.Length != 0)
                    {
                        var messageId = message.MessageId.ToString("N");
                        var version = entry.ExpectedVersion;
                        Array.ForEach(events, @event => enricher(@event));
                        var messages = Array.ConvertAll(
                            events,
                            @event =>
                            new NewStreamMessage(
                                Deterministic.Create(Deterministic.Namespaces.Events,
                                                     $"{messageId}-{version++}"),
                                EventMapping.GetEventName(@event.GetType()),
                                JsonConvert.SerializeObject(@event, SerializerSettings)
                                ));
                        await store.AppendToStream(entry.Stream, entry.ExpectedVersion, messages, ct);
                    }
                }
            }
                                                       ));
        }