Пример #1
0
        public async Task PublishEvent <TEvent, TPipeContext>(TEvent @event) where TEvent : IEvent
        {
            if (_serviceProvider.GetServices(typeof(IEventHandler <TEvent>)) is IEnumerable <IEventHandler <TEvent> > handlers)
            {
                var eventHandlers = handlers as IEventHandler <TEvent>[] ?? handlers.ToArray();
                if (eventHandlers.Any())
                {
                    foreach (var handler in eventHandlers)
                    {
                        await handler.Handle(@event);
                    }
                }
            }

            var pipeContext = new EventPipelineContext <TEvent, TPipeContext>(@event);

            var pipelines = _serviceProvider.GetServices(typeof(IEventPipeline <TEvent, TPipeContext>));

            foreach (IEventPipeline <TEvent, TPipeContext> pipeline in pipelines)
            {
                var builder = new EventPipelineBuilder <TEvent, TPipeContext>();
                pipeline.Configure(builder);
                await ExecutePipeline(builder.GetPipes(), pipeContext);
            }
        }
Пример #2
0
 public async Task ExecuteEventPipeline <TEvent>(TEvent @event, Type pipelineType) where TEvent : IEvent
 {
     if (_serviceProvider.GetService(pipelineType) is IEventPipeline <TEvent> pipeline)
     {
         var pipeContext = new EventPipelineContext <TEvent>(@event);
         var builder     = new EventPipelineBuilder <TEvent>();
         pipeline.Configure(builder);
         await ExecutePipeline(builder.GetPipes(), pipeContext);
     }
     else
     {
         throw new InvalidOperationException($"{pipelineType.Name} is not of type {nameof(IEventPipeline<TEvent>)}");
     }
 }
Пример #3
0
        private async Task ExecutePipeline <TEvent, TContext>(Type[] pipes, EventPipelineContext <TEvent, TContext> context) where TEvent : IEvent
        {
            if (pipes.Any() == false)
            {
                return;
            }

            var pipeType = pipes.First();
            var pipeObj  = _serviceProvider.GetRequiredService(pipeType) as IEventPipe <TEvent, TContext>;

            if (pipeObj == null)
            {
                throw new InvalidOperationException($"{pipeType.FullName} is not a valid IPipe");
            }

            await pipeObj.Handle(context, async (p2) =>
            {
                if (pipes.Length > 1)
                {
                    await ExecutePipeline(pipes.Skip(1).ToArray(), p2);
                }
            });
        }