private async Task ExecutePipeline <TRequest>(Type[] pipes, CommandPipelineContext <TRequest> commandPipelineContext) where TRequest : ICommand { if (pipes.Any() == false) { return; } var pipeType = pipes.First(); var pipeObj = _serviceProvider.GetRequiredService(pipeType) as ICommandPipe <TRequest>; if (pipeObj == null) { throw new InvalidOperationException($"{pipeType.FullName} is not a valid IPipe"); } await pipeObj.Handle(commandPipelineContext, async (p2) => { if (pipes.Length > 1) { await ExecutePipeline(pipes.Skip(1).ToArray(), p2); } }); }
public async Task ExecuteCommand <TRequest, TPipeContext>(TRequest request) where TRequest : ICommand { if (_serviceProvider.GetService(typeof(ICommandHandler <TRequest>)) is ICommandHandler <TRequest> handler) { await handler.Handle(request); return; } var pipeContext = new CommandPipelineContext <TRequest, TPipeContext>(request); var builder = new CommandPipelineBuilder <TRequest, TPipeContext>(); var pipeline = _serviceProvider.GetRequiredService(typeof(ICommandPipeline <TRequest, TPipeContext>)) as ICommandPipeline <TRequest, TPipeContext>; if (pipeline == null) { throw new InvalidOperationException($"Pipeline does not exist for {request.GetType().FullName}"); } pipeline.Configure(builder); await ExecutePipeline(builder.GetPipes(), pipeContext); }