Created
December 8, 2025 07:03
-
-
Save PiotrFerenc/21eec64be4ab9667624b432398235f30 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| await PipelineFactory.Create<SampleContext>() | |
| .Use(x => new ConsoleActionContext | |
| { | |
| Message = x.Name | |
| }, new ConsoleAction()) | |
| .ExecuteAsync(new SampleContext | |
| { | |
| Name = "test" | |
| }); | |
| public static class PipelineFactory | |
| { | |
| public static Pipeline<TContext> Create<TContext>() => new Pipeline<TContext>(); | |
| } | |
| public class ActionParams | |
| { | |
| } | |
| public class Pipeline<TContext> | |
| { | |
| private readonly Queue<Func<TContext, Task>> _queue = new(); | |
| public Pipeline<TContext> Use<TNextContext>(Func<TContext, TNextContext> mapToCtx, PipelineAction<TNextContext> action) where TNextContext : Context | |
| { | |
| if (mapToCtx is null) throw new ArgumentNullException(nameof(mapToCtx)); | |
| if (action is null) throw new ArgumentNullException(nameof(action)); | |
| _queue.Enqueue(ctx => action.InvokeAsync(mapToCtx(ctx))); | |
| return this; | |
| } | |
| public async Task ExecuteAsync(TContext context) | |
| { | |
| while (_queue.Count != 0) | |
| { | |
| var step = _queue.Dequeue(); | |
| await step(context); | |
| } | |
| } | |
| } | |
| public interface IPipelineAction<in TContext> | |
| { | |
| Task InvokeAsync(TContext context); | |
| } | |
| public abstract class PipelineAction<TContext> : IPipelineAction<TContext> | |
| { | |
| public abstract Task InvokeAsync(TContext context); | |
| } | |
| public class ConsoleAction : PipelineAction<ConsoleActionContext> | |
| { | |
| public override Task InvokeAsync(ConsoleActionContext context) | |
| { | |
| Console.WriteLine(context.Message); | |
| return Task.CompletedTask; | |
| ; | |
| } | |
| } | |
| public abstract class Context | |
| { | |
| } | |
| public class ConsoleActionContext : Context | |
| { | |
| public string Message { get; set; } | |
| } | |
| public class SampleContext : Context | |
| { | |
| public string Name { get; set; } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment