|
| 1 | + |
| 2 | + |
| 3 | +# Execution |
| 4 | + |
| 5 | +| Method | Description |
| 6 | +| ---------- | ----------- |
| 7 | +| Call | Execute a `void` step that does not transform the pipeline output. |
| 8 | +| CallAsync | Asynchronously execute a `void` step that does not transform the pipeline output. |
| 9 | +| Pipe | Execute a step that transforms the pipeline output. |
| 10 | +| PipeAsync | Asynchronously execute a step that transforms the pipeline output. |
| 11 | + |
| 12 | +## Flow Control |
| 13 | + |
| 14 | +| Method | Description |
| 15 | +| ---------- | ----------- |
| 16 | +| Cancel | Cancels the pipeline after the current step. |
| 17 | +| CancelWith | Cancels the pipeline with a value, after the current step. |
| 18 | +| Pipe | Pipes a child pipeline with optional middlewares. |
| 19 | +| PipeIf | Conditionally pipes a child pipeline with optional middlewares. |
| 20 | +| Call | Calls a child pipeline with optional middlewares. |
| 21 | +| CallIf | Conditionally calls a child pipeline with optional middlewares. |
| 22 | +| ForEach | Enumerates a collection pipeline input. |
| 23 | +| Reduce | Transforms an enumerable pipeline input. |
| 24 | + |
| 25 | +## Parallel Flow |
| 26 | + |
| 27 | +| Method | Description |
| 28 | +| ---------- | ----------- |
| 29 | +| WaitAll | Waits for concurrent pipelines to complete. |
| 30 | + |
| 31 | +## Middleware |
| 32 | + |
| 33 | +| Method | Description |
| 34 | +| ---------- | ----------- |
| 35 | +| Hook | Applies middleware to each step in the pipeline. |
| 36 | +| Wrap | Wraps the middleware around the preceeding steps. |
| 37 | + |
| 38 | +## Building and Executing Pipelines |
| 39 | + |
| 40 | +Pipelines are built using `PipelineFactory`. Once built, a pipeline is just an async function that takes a `PipelineContext` and |
| 41 | +an optional input value as parameters, and returns a result. |
| 42 | + |
| 43 | +```csharp |
| 44 | + var command = PipelineFactory |
| 45 | + .Start<string>() |
| 46 | + .Pipe( ( ctx, arg ) => $"hello {arg}" ) |
| 47 | + .Build(); |
| 48 | + |
| 49 | + var result = await command( new PipelineContext(), "pipeline" ); |
| 50 | + |
| 51 | + Assert.AreEqual( "hello pipeline", result ); |
| 52 | +``` |
| 53 | + |
| 54 | +## Command Pattern |
| 55 | + |
| 56 | +`ICommand*` interfaces and `Command*` base classes provide a lightweight pattern for constructing injectable commands built |
| 57 | + around pipelines and middleware. |
| 58 | + |
| 59 | +| Interface | Class | Description |
| 60 | +| -------------------------------------- | ------------------------------------- | --------------------------------------------------- |
| 61 | +| ICommandFunction<TInput,TOutput> | CommandFunction<TInput,TOutput> | A command that takes an input and returns an output |
| 62 | +| ICommandFunction<TOutput> | CommandFunction<TOutput> | A command that takes no input and returns an output |
| 63 | +| ICommandProcedure<TInput> | CommandProcedure<TInput> | A command that takes an input and returns void |
| 64 | + |
| 65 | +#### Example 1 |
| 66 | +Example of a command that takes an input and produces an output. |
| 67 | + |
| 68 | +```csharp |
| 69 | + public interface IMyCommand : ICommandFunction<Guid, String> |
| 70 | + { |
| 71 | + } |
| 72 | + |
| 73 | + public class MyCommand : CommandFunction<Guid, String>, IMyCommand |
| 74 | + { |
| 75 | + public MyCommand( ILogger<GetMessageCommand> logger ) |
| 76 | + : base( logger) |
| 77 | + { |
| 78 | + } |
| 79 | + |
| 80 | + protected override FunctionAsync<Guid, String> PipelineFactory() |
| 81 | + { |
| 82 | + return PipelineFactory |
| 83 | + .Start<Guid>() |
| 84 | + .WithLogging() |
| 85 | + .Pipe( GetString ) |
| 86 | + .Build(); |
| 87 | + } |
| 88 | + |
| 89 | + private async Task<String> GetString( IPipelineContext context, Guid id ) |
| 90 | + { |
| 91 | + return id.ToString(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // usage |
| 96 | + void usage( IMyCommand command ) |
| 97 | + { |
| 98 | + var result = await command.ExecuteAsync( Guid.Create() ); // takes a Guid, returns a string |
| 99 | + } |
| 100 | +``` |
| 101 | + |
| 102 | +#### Example 2 |
| 103 | +Example of a command that takes no input and produces an output. |
| 104 | + |
| 105 | +```csharp |
| 106 | + public interface IMyCommand : ICommandFunction<String> |
| 107 | + { |
| 108 | + } |
| 109 | + |
| 110 | + public class MyCommand : CommandFunction<String>, IMyCommand |
| 111 | + { |
| 112 | + public MyCommand( ILogger<GetMessageCommand> logger ) |
| 113 | + : base( logger) |
| 114 | + { |
| 115 | + } |
| 116 | + |
| 117 | + protected override FunctionAsync<Arg.Empty, String> CreatePipeline() |
| 118 | + { |
| 119 | + return PipelineFactory |
| 120 | + .Start<Arg.Empty>() |
| 121 | + .WithLogging() |
| 122 | + .PipeAsync( GetString ) |
| 123 | + .Build(); |
| 124 | + } |
| 125 | + |
| 126 | + private String GetString( IPipelineContext context, Arg.Empty _ ) |
| 127 | + { |
| 128 | + return "Hello"; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // usage |
| 133 | + void usage( IMyCommand command ) |
| 134 | + { |
| 135 | + var result = await command.ExecuteAsync(); // returns "Hello" |
| 136 | + } |
| 137 | +``` |
| 138 | + |
| 139 | +#### Example 3 |
| 140 | +Example of a command that takes an input and produces no output. |
| 141 | + |
| 142 | +```csharp |
| 143 | + public interface IMyCommand : ICommandProcedure<String> |
| 144 | + { |
| 145 | + } |
| 146 | + |
| 147 | + public class MyCommand : CommandProcedure<String>, IMyCommand |
| 148 | + { |
| 149 | + public GetCommand( ILogger<MyCommand> logger ) |
| 150 | + : base( logger) |
| 151 | + { |
| 152 | + } |
| 153 | + |
| 154 | + protected override ProcedureAsync<String> CreatePipeline() |
| 155 | + { |
| 156 | + return PipelineFactory |
| 157 | + .Start<String>() |
| 158 | + .WithLogging() |
| 159 | + .PipeAsync( ExecSomeAction ) |
| 160 | + .BuildAsProcedure(); |
| 161 | + } |
| 162 | + |
| 163 | + private String ExecSomeAction( IPipelineContext context, String who ) |
| 164 | + { |
| 165 | + return $"Hello {who}"; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // usage |
| 170 | + void usage( IMyCommand command ) |
| 171 | + { |
| 172 | + var result = await command.ExecuteAsync( "me" ); // returns "Hello me" |
| 173 | + } |
| 174 | +``` |
0 commit comments