|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Advanced Patterns |
| 4 | +nav_order: 7 |
| 5 | +--- |
| 6 | + |
| 7 | +# Advanced Pipeline Patterns |
| 8 | + |
| 9 | +This page demonstrates advanced usage of the Hyperbee Pipeline library, combining extension methods, custom binders, and middleware for powerful and flexible pipeline composition. |
| 10 | + |
| 11 | +## Combining Custom Middleware, Extension Methods, and a Binder |
| 12 | + |
| 13 | +Suppose you want to: |
| 14 | + |
| 15 | +- Add logging to every step (middleware) |
| 16 | +- Add a custom step via an extension method |
| 17 | +- Use a custom binder to control flow (e.g., retry on failure) |
| 18 | + |
| 19 | +### Custom Logging Middleware |
| 20 | + |
| 21 | +```csharp |
| 22 | +public static class PipelineMiddleware |
| 23 | +{ |
| 24 | + public static IPipelineStartBuilder<TStart, TOutput> WithLogging<TStart, TOutput>(this IPipelineStartBuilder<TStart, TOutput> builder) |
| 25 | + { |
| 26 | + return builder.HookAsync(async (ctx, arg, next) => |
| 27 | + { |
| 28 | + Console.WriteLine($"[LOG] Before: {arg}"); |
| 29 | + var result = await next(ctx, arg); |
| 30 | + Console.WriteLine($"[LOG] After: {result}"); |
| 31 | + return result; |
| 32 | + }); |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +### Custom Step via Extension Method |
| 38 | + |
| 39 | +```csharp |
| 40 | +public static class PipelineExtensions |
| 41 | +{ |
| 42 | + public static IPipelineBuilder<TStart, TOutput> WithCustomTransform<TStart, TOutput>( |
| 43 | + this IPipelineBuilder<TStart, TOutput> builder, Func<TOutput, TOutput> transform) |
| 44 | + { |
| 45 | + return builder.Pipe((ctx, arg) => transform(arg)); |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +### Custom Retry Binder |
| 51 | + |
| 52 | +```csharp |
| 53 | +public class RetryBinder<TStart, TOutput> : Binder<TStart, TOutput> |
| 54 | +{ |
| 55 | + private readonly int _maxRetries; |
| 56 | + public RetryBinder(FunctionAsync<TStart, TOutput> function, int maxRetries = 3) |
| 57 | + : base(function, null) => _maxRetries = maxRetries; |
| 58 | + |
| 59 | + public FunctionAsync<TStart, TOutput> Bind(FunctionAsync<TStart, TOutput> next) |
| 60 | + { |
| 61 | + return async (context, argument) => |
| 62 | + { |
| 63 | + int attempt = 0; |
| 64 | + while (true) |
| 65 | + { |
| 66 | + try { return await next(context, argument); } |
| 67 | + catch when (++attempt < _maxRetries) { } |
| 68 | + } |
| 69 | + }; |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### Usage Example |
| 75 | + |
| 76 | +```csharp |
| 77 | +var pipeline = PipelineFactory |
| 78 | + .Start<string>() |
| 79 | + .WithLogging() |
| 80 | + .Pipe((ctx, arg) => arg + " step1") |
| 81 | + .WithCustomTransform(s => s.ToUpper()) |
| 82 | + .Pipe((ctx, arg) => arg + " step2") |
| 83 | + .Pipe((ctx, arg) => throw new Exception("fail")) |
| 84 | + .Pipe((ctx, arg) => new RetryBinder<string, string>(null, 3).Bind((c, a) => Task.FromResult(a))) |
| 85 | + .Build(); |
| 86 | + |
| 87 | +var result = await pipeline(new PipelineContext(), "input"); |
| 88 | +``` |
| 89 | + |
| 90 | +This example demonstrates how to combine middleware, extension methods, and a custom binder for advanced scenarios. |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +For more, see [Extending Pipelines](extending.md) and [Middleware](middleware.md). |
0 commit comments