concept pipeline in category .net

This is an excerpt from Manning's book Concurrency in .NET.
13.2.1 Solution: composing a pipeline of steps forming the Fork/Join pattern
With TDF, you can compose different building blocks together as a pipeline. You can use the pipeline to define the steps of a Fork/Join pattern (figure 13.5), where the Fork step runs a set of tasks in parallel, then the following step joins the results, and the final step applies a reducer block for the ultimate output. For the later step of the workflow that aggregates the results, you need an object that maintains the state of the previous steps. In this case, you use the agent-based block built in chapter 12 using TDF.
Listing 13.2 Parallel
ForkJoinusing TDFpublic static async Task<R> ForkJoin<T1, T2, R>( this IEnumerable<T1> source, Func<T1, Task<IEnumerable<T2>>> map, #1 Func<R, T2, Task<R>> aggregate, #1 R initialState, CancellationTokenSource cts = null, int partitionLevel = 8, int boundCapacity = 20) #2 { cts = cts ?? new CancellationTokenSource(); var blockOptions = new ExecutionDataflowBlockOptions { #3 MaxDegreeOfParallelism = partitionLevel, BoundedCapacity = boundCapacity, CancellationToken = cts.Token }; var inputBuffer = new BufferBlock<T1>( #4 new DataflowBlockOptions { CancellationToken = cts.Token, BoundedCapacity = boundCapacity }); var mapperBlock = new TransformManyBlock<T1, T2> ➥ (map, blockOptions); #4 var reducerAgent = Agent.Start(initialState, aggregate, cts); #4 var linkOptions = new DataflowLinkOptions{PropagateCompletion=true}; inputBuffer.LinkTo(mapperBlock, linkOptions); #5 IDisposable disposable = mapperBlock.AsObservable() .Subscribe(async item => await reducerAgent.Send(item)); #6 foreach (var item in source) await inputBuffer.SendAsync(item); #7 inputBuffer.Complete(); var tcs = new TaskCompletionSource<R>(); await inputBuffer.Completion.ContinueWith(task => mapperBlock.Complete()); await mapperBlock.Completion.ContinueWith(task => { #8 var agent = reducerAgent as StatefulDataflowAgent<R, T2>; disposable.Dispose(); tcs.SetResult(agent.State); }); return await tcs.Task; } #1 The functions map and aggregate return a Task type to ensure concurrent behavior. #2 The partitionLevel is set to a default value of 8 and the boundCapacity to 20; these are arbitrary values that can, and should, be updated according to your needs. #3 Instances of the building blocks that are shaping the Fork/Join pipeline #4 Packages up the properties of the execution details to configure the inputBuffer BufferBlock #5 Connects the building blocks to form and link the steps to run the Fork/Join pattern #6 The TransformManyBlock is transformed into an Observable, which is used to push the outputs as a message to the reducerAgent. #7 Starts the Fork/Join process by pushing the items of the input collection into the first step of the pipeline #8 When the mapperBlock is completed, the continuation Task sets the reducerAgent as a result for the tcs Task passed to the caller as output. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":28},{\"line\":0,\"ch\":36}]]"} !@%STYLE%@!
At a high level, you can think of this composition over monadic functions as a pipeline, where the result of the first function is piped into the next one and so on until the last step. In general, when you think of piping, you can think of two approaches: applicative (
<*>) and monadic (>>=). Because you need the result of the previous call in your next call, the monadic style (>>=) is the better choice.For this example, you use the
TamingAgentfrom the previous recipe. TheTamingAgenthas the method memberAsk, whose signature matches the scenario, where it takes a generic argument'Tand returns anAsync<'R>type. At this point, you use theKleislioperator to compose a set ofTamingAgenttypes to form a pipeline of agents, as shown in figure 13.18. The result of each agent is computed independently and passed as input, in the form of a message, to the next agent until the last node of the chain performs the final side effect. The technique of linking and composing agents can lead to robust designs and concurrent systems. When an agent returns (replies back) a result to the caller, it can be composed into a pipeline of agents.Figure 13.18 The pipeline processing pattern is useful when you want to process data in multiple steps. The idea behind the pattern is that inputs are sent to the first agent in the pipeline. The main benefit of the pipeline processing pattern is that it provides a simple way to balance the tradeoff between overly sequential processing (which may reduce performance) and overly parallel processing (which may have a large overhead).
![]()

This is an excerpt from Manning's book Microservices in .NET Core: with examples in Nancy.
Now that you have a minimal Nancy application up and running, you can use a piece of OWIN middleware to add some simple request logging. Your application already has an OWIN pipeline, albeit a short one. It contains only one component: Nancy. The code in listing 1.4, which goes in the Startup class, adds to the pipeline a component that writes “Got request” to the console every time a request comes in. It then passes the request to the next component in the pipeline: Nancy.
Figure 8.3. You can use a pipeline of middleware between the web server and the endpoint handler to handle cross-cutting concerns.
![]()
The pieces of code between the web server and the endpoint handlers form a pipeline: every request flows through each piece in turn before reaching the endpoint handler in the Nancy module. Likewise, the response from the endpoint handler flows back through the same pipeline before reaching the web server, which sends the response back to the caller. In this chapter, you’ll use OWIN to implement such a pipeline; each piece of the pipeline is called OWIN middleware.
Call the pipeline with the OWIN environment set up at the beginning of the test.

This is an excerpt from Manning's book IronPython in Action.
The RunspaceInvoke instances are great for executing individual commands, but you can achieve more by creating a pipeline. This gets you, in effect, a PowerShell environment embedded into IronPython. Listing 10.15 creates a pipeline, adds commands to it, and then invokes the whole pipeline.