TPL dataflow: flattens the incoming collection for sequential elements

I am creating an application using TPL data stream. Actually I have the following problem. I have one transformer var tfb1 = new TranformBlock<InMsg, IReadOnlyCollection<OutMsg>>

. Thus, it tfb1

gets in the message and creates a list of outgoing messages. This outgoing message list must be associated with the router data block that it receives OutMsg

as input (not IReadOnlyCollection<OutMsg>

).

How can be smoothed IReadOnlyCollection

so that the containing message can be used as an input for, for example, a transform block as TransformBlock<OutMsg, SomeOtherType>

. Is it possible through LinkTo()

?

thank

+4


source to share


1 answer


You can use TransformManyBlock

instead TransformMany

to flatten any result IEnumerable<T>

, for example:

var flattenBlock = new TransformManyBlock<InMsg,OutMsg>(msg=>{
    List<OutMsg> myResultList;
    //Calculate some results
    return myResultList;
});

      

This will pass the individual OutMsg instances to the next block.

You can use an iterator to propagate individual messages at once:

  var flattenBlock = new TransformManyBlock<InMsg,OutMsg>(msg=>{
    //Calculate some results
    foreach(var item in someDbResults)
    {
        yield return item;
    }
});

      

Or you can just flatten the input:



var flattenBlock = new TransformManyBlock<IEnumerable<OutMsg>,OutMsg>(items=>items);

      

You can link this block to any block that accepts OutMsg:

var block = new ActionBlock<OutMsg>(...);

flattenBlock.LinkTo(block);

      

You can route messages by passing a predicate to LinkTo

. For example, if you want to redirect error messages to a logging block, you can type:

flattenBlock.LinkTo(logBlock,msg=>msg.HasError);
flattenBlock.LinkTo(happyBlock);

      

Messages that do not match the predicate will get stuck in the output buffer and prevent the block from completing. By having a single link without a predicate, you ensure that all messages are processed

+3


source







All Articles