Chapter 5. Dataflow Basics
TPL Dataflow is a powerful library that enables you to create a mesh or pipeline and then (asynchronously) send your data through it. Dataflow is a very declarative style of coding: normally, you completely define the mesh first and then start processing data. The mesh ends up being a structure through which your data flows. This requires you to think about your application a bit differently, but once you make that leap, dataflow becomes a natural fit for many scenarios.
Each mesh is comprised of various blocks that are linked to each other. The individual blocks are simple and are responsible for a single step in the data processing. When a block finishes working on its data, it will pass its result along to any linked blocks.
To use TPL Dataflow, install the NuGet package System.Threading.Tasks.Dataflow
into your application.
5.1 Linking Blocks
Problem
You need to link dataflow blocks to one another to create a mesh.
Solution
The blocks provided by the TPL Dataflow library define only the most basic members. Many of the useful TPL Dataflow methods are actually extension methods. The LinkTo
extension method provides an easy way to link dataflow blocks together:
var
multiplyBlock
=
new
TransformBlock
<
int
,
int
>(
item
=>
item
*
2
);
var
subtractBlock
=
new
TransformBlock
<
int
,
int
>(
item
=>
item
-
2
);
// After linking, values that exit multiplyBlock will enter subtractBlock.
multiplyBlock
.
LinkTo
(
subtractBlock
);
By default, linked dataflow blocks only propagate ...
Get Concurrency in C# Cookbook, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.