Search code examples
c#.nettpl-dataflow

Correct usage of TplDataFlow TransformBlock


I have a scenario in which I will have to transform a large number of objects and assign them to some parent container objects.

I use an id on these items in order to determine which parent they belong to, so I have decided that I will create a Dictionary in order organise each parent object and its children.

My question is regarding the correct usage of the TransformBlock, should I try to find a child's parent object and assign it to the dictionary like I am doing in the example below sample code or should the transform block only take care of transforming the object and the actual job of finding and assigning the parent be done in an action block which is linked to the transform block.

var dictionary = new Dictionary<int, List<Item>>
{
    {1,new List<Item>() },
    {2,new List<Item>() },
    {3,new List<Item>() },
    {4,new List<Item>() },
    {5,new List<Item>() },
    {6,new List<Item>() },
    {7,new List<Item>() },
    {8,new List<Item>() },
    {9,new List<Item>() },
    {10,new List<Item>() },
};   

var transformBlock = new TransformBlock<ItemToMap, Item>
    (
        (itemToMap) =>
        {
            var parent = dictionary[itemToMap.ParentId];
            var item = mapToItem(itemToMap);
            parent.Add(item);
            return item;
        }
    );

foreach (var itemToMap in AllItemsToMap)
{
    transformBlock.Post(itemToMap);
}

transformBlock.Complete();

Solution

  • As you're using it right now there's no reason to use a TransformBlock. Use ActionBlock instead.

    On whether to split the work into two different blocks, my answer would be that it depends on your configuration. If you want different levels of parallelism for example you can split the work into blocks.

    In your example however, I don't see a reason to use a block at all. There's no real work being done other than inserting items to the dictionary.