using System.Collections.Generic; using Microsoft.Extensions.Logging; namespace CellularManagement.WebSocket.Pipeline; public class PipelineBuilder { private readonly List> _steps = new(); private readonly ILoggerFactory _loggerFactory; public PipelineBuilder(ILoggerFactory loggerFactory) { _loggerFactory = loggerFactory; } public PipelineBuilder AddStep(IPipelineStep step) { if (step == null) { throw new ArgumentNullException(nameof(step)); } _steps.Add(step); return this; } public IPipelineStep Build() { if (_steps.Count == 0) { throw new InvalidOperationException("Pipeline must have at least one step"); } return new Pipeline(_steps, _loggerFactory.CreateLogger>()); } } public class Pipeline : IPipelineStep { private readonly IReadOnlyList> _steps; private readonly ILogger> _logger; public Pipeline(IReadOnlyList> steps, ILogger> logger) { _steps = steps; _logger = logger; } public async Task ProcessAsync(TInput input, CancellationToken cancellationToken) { var current = input; TOutput output = default!; foreach (var step in _steps) { output = await step.ProcessAsync(current, cancellationToken); current = (TInput)(object)output!; } return output; } public async Task ProcessWithErrorHandlingAsync(TInput input, CancellationToken cancellationToken) { try { return await ProcessAsync(input, cancellationToken); } catch (Exception ex) { _logger.LogError(ex, "Error in pipeline processing"); throw new PipelineException("Pipeline processing failed", ex); } } }