Class PipelineBuilderExtensions
- Namespace
- DotnetKit.DataflowBuilder
- Assembly
- DotnetKit.DataflowBuilder.dll
Provides fluent extension methods for configuring stages, transformations, batching, grouping, and targets on pipeline builders.
public static class PipelineBuilderExtensions
- Inheritance
-
PipelineBuilderExtensions
- Inherited Members
Methods
Batch<TSource, TInput>(IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>, int)
Appends a batching block (BatchBlock<T>) to the pipeline, grouping incoming elements into arrays of fixed size.
public static IFluentBuilder<ISourceBlock<TInput[]>, IPipelineBuilder<TSource>> Batch<TSource, TInput>(this IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> pipelineBuilder, int batchSize = 100)
Parameters
pipelineBuilderIFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>The fluent pipeline builder instance.
batchSizeintThe maximum number of items per batch. Defaults to 100.
Returns
- IFluentBuilder<ISourceBlock<TInput[]>, IPipelineBuilder<TSource>>
A fluent builder positioned at the output of the batch block yielding arrays of
TInput.
Type Parameters
TSourceThe entry element type of the pipeline.
TInputThe incoming element type to be batched.
Examples
builder.Batch(batchSize: 50);
Batch<TSource, TInput>(IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>, int, TimeSpan)
Appends a temporal batching block (TimeBatchBlock<T>) to the pipeline,
grouping elements into batches of up to batchSize items,
or flushing sooner if timeout expires since the first buffered element.
public static IFluentBuilder<ISourceBlock<TInput[]>, IPipelineBuilder<TSource>> Batch<TSource, TInput>(this IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> pipelineBuilder, int batchSize, TimeSpan timeout)
Parameters
pipelineBuilderIFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>The fluent pipeline builder instance.
batchSizeintThe maximum number of items per batch.
timeoutTimeSpanThe maximum time to wait before emitting a partial batch.
Returns
- IFluentBuilder<ISourceBlock<TInput[]>, IPipelineBuilder<TSource>>
A fluent builder yielding arrays of
TInput.
Type Parameters
TSourceThe entry element type of the pipeline.
TInputThe incoming element type to be batched.
Build<TSource, TInput>(IFluentBuilder<ITargetBlock<TInput>, IPipelineBuilder<TSource>>)
Builds and finalizes a single-source dataflow pipeline from the configured builder stages.
public static IPipeline<TSource> Build<TSource, TInput>(this IFluentBuilder<ITargetBlock<TInput>, IPipelineBuilder<TSource>> pipelineBuilder)
Parameters
pipelineBuilderIFluentBuilder<ITargetBlock<TInput>, IPipelineBuilder<TSource>>The fluent builder terminating at a target block.
Returns
- IPipeline<TSource>
An executable IPipeline<TSource> instance.
Type Parameters
TSourceThe entry element type of the pipeline.
TInputThe element type consumed at the final target stage.
Build<TSource1, TSource2, TInput>(IFluentBuilder<ITargetBlock<TInput>, IPipelineBuilder<Tuple<TSource1, TSource2>>>)
Builds and finalizes a multi-source dataflow pipeline from the configured builder stages.
public static IPipeline<TSource1, TSource2> Build<TSource1, TSource2, TInput>(this IFluentBuilder<ITargetBlock<TInput>, IPipelineBuilder<Tuple<TSource1, TSource2>>> pipelineBuilder)
Parameters
pipelineBuilderIFluentBuilder<ITargetBlock<TInput>, IPipelineBuilder<Tuple<TSource1, TSource2>>>The fluent builder terminating at a target block.
Returns
- IPipeline<TSource1, TSource2>
An executable IPipeline<TSource1, TSource2> instance.
Type Parameters
TSource1The first entry element type of the pipeline.
TSource2The second entry element type of the pipeline.
TInputThe element type consumed at the final target stage.
Exceptions
- InvalidOperationException
Thrown if the underlying builder is not a multi-source builder.
FilterAsync<TSource, TInput>(IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>, Func<TInput, Task<bool>>, int)
Filters pipeline items asynchronously, allowing only items that satisfy predicate to pass downstream.
public static IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> FilterAsync<TSource, TInput>(this IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> pipelineBuilder, Func<TInput, Task<bool>> predicate, int maxDegreeOfParallelism = 1)
Parameters
pipelineBuilderIFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>The fluent pipeline builder instance.
predicateFunc<TInput, Task<bool>>An asynchronous function testing each item for a condition.
maxDegreeOfParallelismintThe maximum degree of parallel processing. Defaults to 1.
Returns
- IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>
A fluent builder positioned at the output of the filter stage.
Type Parameters
TSourceThe entry element type of the pipeline.
TInputThe incoming element type to filter.
Filter<TSource, TInput>(IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>, Func<TInput, bool>, int)
Filters pipeline items synchronously, allowing only items that satisfy predicate to pass downstream.
public static IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> Filter<TSource, TInput>(this IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> pipelineBuilder, Func<TInput, bool> predicate, int maxDegreeOfParallelism = 1)
Parameters
pipelineBuilderIFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>The fluent pipeline builder instance.
predicateFunc<TInput, bool>A function testing each item for a condition.
maxDegreeOfParallelismintThe maximum degree of parallel processing. Defaults to 1.
Returns
- IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>
A fluent builder positioned at the output of the filter stage.
Type Parameters
TSourceThe entry element type of the pipeline.
TInputThe incoming element type to filter.
Group<TSource, TGroupKey, TInput>(IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>, Func<TInput, TGroupKey>, int, int)
Appends a grouping block (GroupBlock<TGroupKey, TInput>) to the pipeline, grouping elements by a key selector and emitting batches when each group's capacity is reached or upon pipeline completion.
public static IFluentBuilder<ISourceBlock<TInput[]>, IPipelineBuilder<TSource>> Group<TSource, TGroupKey, TInput>(this IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> pipelineBuilder, Func<TInput, TGroupKey> groupingSelector, int boundedCapacity = 100, int maxDegreeOfParallelism = 1) where TGroupKey : notnull
Parameters
pipelineBuilderIFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>The fluent pipeline builder instance.
groupingSelectorFunc<TInput, TGroupKey>A function extracting the grouping key from each element.
boundedCapacityintThe maximum number of items per group before a batch is emitted downstream. Defaults to 100.
maxDegreeOfParallelismintThe maximum degree of parallelism. Defaults to 1.
Returns
- IFluentBuilder<ISourceBlock<TInput[]>, IPipelineBuilder<TSource>>
A fluent builder positioned at the output of the grouping block yielding arrays of
TInput.
Type Parameters
TSourceThe entry element type of the pipeline.
TGroupKeyThe non-nullable key type used for grouping.
TInputThe incoming element type to be grouped.
ProcessAsync<TSource, TInput, TOutput>(IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>, Func<TInput, Task<TOutput>>, int)
Appends an asynchronous transformation block (TransformBlock<TInput, TOutput>) to the pipeline.
public static IFluentBuilder<ISourceBlock<TOutput>, IPipelineBuilder<TSource>> ProcessAsync<TSource, TInput, TOutput>(this IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> pipelineBuilder, Func<TInput, Task<TOutput>> propagatorFunc, int maxDegreeOfParallelism = 1)
Parameters
pipelineBuilderIFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>The fluent pipeline builder instance.
propagatorFuncFunc<TInput, Task<TOutput>>The asynchronous transformation function mapping each input to a task producing an output.
maxDegreeOfParallelismintThe maximum degree of parallel processing. Defaults to 1.
Returns
- IFluentBuilder<ISourceBlock<TOutput>, IPipelineBuilder<TSource>>
A fluent builder positioned at the output of the new transformation block.
Type Parameters
TSourceThe entry element type of the pipeline.
TInputThe incoming element type to be transformed.
TOutputThe transformed element type emitted downstream.
Examples
builder.ProcessAsync(async id => await repository.GetByIdAsync(id), maxDegreeOfParallelism: 4);
ProcessAsync<TSource, TInput, TOutput>(IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>, Func<TInput, Task<TOutput>>, int, int, TimeSpan?, Action<TInput, Exception>?)
Appends an asynchronous transformation block with configurable retry resilience.
public static IFluentBuilder<ISourceBlock<TOutput>, IPipelineBuilder<TSource>> ProcessAsync<TSource, TInput, TOutput>(this IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> pipelineBuilder, Func<TInput, Task<TOutput>> propagatorFunc, int maxDegreeOfParallelism, int retryCount, TimeSpan? retryDelay = null, Action<TInput, Exception>? onFailed = null)
Parameters
pipelineBuilderIFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>The fluent pipeline builder instance.
propagatorFuncFunc<TInput, Task<TOutput>>The asynchronous transformation function.
maxDegreeOfParallelismintThe maximum degree of parallel processing.
retryCountintNumber of retry attempts upon failure.
retryDelayTimeSpan?Optional delay between retry attempts.
onFailedAction<TInput, Exception>Optional callback invoked with the item and final exception if all retries are exhausted.
Returns
- IFluentBuilder<ISourceBlock<TOutput>, IPipelineBuilder<TSource>>
A fluent builder positioned at the output of the transformation block.
Type Parameters
TSourceThe entry element type of the pipeline.
TInputThe incoming element type to be transformed.
TOutputThe transformed element type emitted downstream.
ProcessManyAsync<TSource, TInput, TOutput>(IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>, Func<TInput, Task<IEnumerable<TOutput>>>, int)
Appends an asynchronous one-to-many transformation block (TransformManyBlock<TInput, TOutput>) to the pipeline, expanding each input item asynchronously into a sequence of output items.
public static IFluentBuilder<ISourceBlock<TOutput>, IPipelineBuilder<TSource>> ProcessManyAsync<TSource, TInput, TOutput>(this IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> pipelineBuilder, Func<TInput, Task<IEnumerable<TOutput>>> propagatorFunc, int maxDegreeOfParallelism = 1)
Parameters
pipelineBuilderIFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>The fluent pipeline builder instance.
propagatorFuncFunc<TInput, Task<IEnumerable<TOutput>>>The asynchronous function mapping each input item to a task returning an enumerable collection of outputs.
maxDegreeOfParallelismintThe maximum degree of parallel processing. Defaults to 1.
Returns
- IFluentBuilder<ISourceBlock<TOutput>, IPipelineBuilder<TSource>>
A fluent builder positioned at the output of the new transform-many block.
Type Parameters
TSourceThe entry element type of the pipeline.
TInputThe incoming element type.
TOutputThe element type of the expanded sequence.
ProcessMany<TSource, TInput, TOutput>(IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>, Func<TInput, IEnumerable<TOutput>>, int)
Appends a synchronous one-to-many transformation block (TransformManyBlock<TInput, TOutput>) to the pipeline, expanding each input item into a sequence of output items.
public static IFluentBuilder<ISourceBlock<TOutput>, IPipelineBuilder<TSource>> ProcessMany<TSource, TInput, TOutput>(this IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> pipelineBuilder, Func<TInput, IEnumerable<TOutput>> propagatorFunc, int maxDegreeOfParallelism = 1)
Parameters
pipelineBuilderIFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>The fluent pipeline builder instance.
propagatorFuncFunc<TInput, IEnumerable<TOutput>>The function mapping each input item to an enumerable collection of output items.
maxDegreeOfParallelismintThe maximum degree of parallel processing. Defaults to 1.
Returns
- IFluentBuilder<ISourceBlock<TOutput>, IPipelineBuilder<TSource>>
A fluent builder positioned at the output of the new transform-many block.
Type Parameters
TSourceThe entry element type of the pipeline.
TInputThe incoming element type.
TOutputThe element type of the expanded sequence.
Examples
builder.ProcessMany(line => line.Split(','));
Process<TSource, TInput, TOutput>(IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>, Func<TInput, TOutput>, int)
Appends a synchronous transformation block (TransformBlock<TInput, TOutput>) to the pipeline.
public static IFluentBuilder<ISourceBlock<TOutput>, IPipelineBuilder<TSource>> Process<TSource, TInput, TOutput>(this IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> pipelineBuilder, Func<TInput, TOutput> propagatorFunc, int maxDegreeOfParallelism = 1)
Parameters
pipelineBuilderIFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>The fluent pipeline builder instance.
propagatorFuncFunc<TInput, TOutput>The synchronous transformation function mapping each input to an output.
maxDegreeOfParallelismintThe maximum degree of parallel processing. Defaults to 1.
Returns
- IFluentBuilder<ISourceBlock<TOutput>, IPipelineBuilder<TSource>>
A fluent builder positioned at the output of the new transformation block.
Type Parameters
TSourceThe entry element type of the pipeline.
TInputThe incoming element type to be transformed.
TOutputThe transformed element type emitted downstream.
Examples
builder.Process(x => x.Trim());
ToChannel<TSource, TInput>(IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>, ChannelWriter<TInput>, bool, int)
Connects the output of the pipeline to a ChannelWriter<T>, forwarding all emitted items into the channel.
public static IFluentBuilder<ITargetBlock<TInput>, IPipelineBuilder<TSource>> ToChannel<TSource, TInput>(this IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> pipelineBuilder, ChannelWriter<TInput> channelWriter, bool completeChannelOnCompletion = true, int maxDegreeOfParallelism = 1)
Parameters
pipelineBuilderIFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>The fluent pipeline builder instance.
channelWriterChannelWriter<TInput>The channel writer receiving items.
completeChannelOnCompletionboolWhether to complete the channel when the pipeline terminates.
maxDegreeOfParallelismintThe maximum degree of parallel channel writes.
Returns
- IFluentBuilder<ITargetBlock<TInput>, IPipelineBuilder<TSource>>
A fluent builder positioned at the terminal block, ready to call
Build().
Type Parameters
TSourceThe entry element type of the pipeline.
TInputThe element type written to the channel.
ToTargetAsync<TSource, TInput>(IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>, Func<TInput, Task>, int)
Appends an asynchronous terminal action block (ActionBlock<TInput>) to consume pipeline items.
public static IFluentBuilder<ITargetBlock<TInput>, IPipelineBuilder<TSource>> ToTargetAsync<TSource, TInput>(this IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> pipelineBuilder, Func<TInput, Task> targetAction, int maxDegreeOfParallelism = 1)
Parameters
pipelineBuilderIFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>The fluent pipeline builder instance.
targetActionFunc<TInput, Task>The asynchronous action executed for each incoming item.
maxDegreeOfParallelismintThe maximum degree of parallel executions. Defaults to 1.
Returns
- IFluentBuilder<ITargetBlock<TInput>, IPipelineBuilder<TSource>>
A fluent builder configured at the target termination step, ready to call
Build().
Type Parameters
TSourceThe entry element type of the pipeline.
TInputThe element type consumed by the target block.
Examples
builder.ToTargetAsync(async item => await database.SaveAsync(item), maxDegreeOfParallelism: 4);
ToTargetAsync<TSource, TInput>(IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>, Func<TInput, Task>, int, int, TimeSpan?, Action<TInput, Exception>?)
Appends an asynchronous terminal action block with configurable retry resilience.
public static IFluentBuilder<ITargetBlock<TInput>, IPipelineBuilder<TSource>> ToTargetAsync<TSource, TInput>(this IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> pipelineBuilder, Func<TInput, Task> targetAction, int maxDegreeOfParallelism, int retryCount, TimeSpan? retryDelay = null, Action<TInput, Exception>? onFailed = null)
Parameters
pipelineBuilderIFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>The fluent pipeline builder instance.
targetActionFunc<TInput, Task>The asynchronous action executed for each incoming item.
maxDegreeOfParallelismintThe maximum degree of parallel executions.
retryCountintNumber of retry attempts upon failure.
retryDelayTimeSpan?Optional delay between retry attempts.
onFailedAction<TInput, Exception>Optional callback invoked with the item and final exception if all retries are exhausted.
Returns
- IFluentBuilder<ITargetBlock<TInput>, IPipelineBuilder<TSource>>
A fluent builder positioned at the target termination step.
Type Parameters
TSourceThe entry element type of the pipeline.
TInputThe element type consumed by the target block.
ToTarget<TSource, TInput>(IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>, Action<TInput>, int)
Appends a synchronous terminal action block (ActionBlock<TInput>) to consume pipeline items.
public static IFluentBuilder<ITargetBlock<TInput>, IPipelineBuilder<TSource>> ToTarget<TSource, TInput>(this IFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>> pipelineBuilder, Action<TInput> targetAction, int maxDegreeOfParallelism = 1)
Parameters
pipelineBuilderIFluentBuilder<ISourceBlock<TInput>, IPipelineBuilder<TSource>>The fluent pipeline builder instance.
targetActionAction<TInput>The synchronous action executed for each incoming item.
maxDegreeOfParallelismintThe maximum degree of parallel executions. Defaults to 1.
Returns
- IFluentBuilder<ITargetBlock<TInput>, IPipelineBuilder<TSource>>
A fluent builder configured at the target termination step, ready to call
Build().
Type Parameters
TSourceThe entry element type of the pipeline.
TInputThe element type consumed by the target block.
Examples
builder.ToTarget(item => Console.WriteLine(item));