viko
viko

Reputation: 79

TPL DataFlow and architecture design

TPL DataFlow library very useful for my application. I have about 10 blocks and I think a count will increase.

When I prepared prototype of my application I was confused because I understood that I got functional design.

void BIG_WORKFLOW_METHOD()
{
    ...
    var block1 = new TransformBlock<string, string>(...);
    var block2 = new TransformBlock<string, string>(...);
    var block3 = new TransformManyBlock<string, string>(...);
    var broadCastBlock = new BroadcastBlock<EventObject>(ev => ev);
    ...
    var block9 = new ActionBlock<string>(...);
    var block10 = new ActionBlock<EventObject>(...);
    block1.LinkTo(block2);
    block2.LinkTo(block3);
    block3.LinkTo(block4);
    broadCastBlock.LinkTo(block5);
    broadCastBlock.LinkTo(block6);
    ...
}

I need to transform my big-workflow-method to OOP-design. I want to be able to add or remove steps from my workflow in future easy. May be somebody solve that task?

I guess the most appropriate architecture for Workflow is State design pattern, but I think TPL DataFlow already use this pattern and that will be over-architect.

Upvotes: 3

Views: 307

Answers (1)

VMAtm
VMAtm

Reputation: 28355

All the questions about design are very broad and hard to be answered with a only one ”silver bullet” solution. If one examine the DataflowBlock extension class, we'll see many functonal-oriented overloads, espesially the one what are dealing with the linking blocks between each other.

So, best thing you can do is introduce some Factory and/or Builder for different types of flows in your application. Such classes easily can construct an easy model for your flow without some low-level lambdas. Here is some thoughts for achieving your goal:

As you already know, blocks can be easily linked between each other, so adding steps for your flow is very easy. Also you can link the blocks with predicate so messages will go directly to specific block you created.
Unlinking the blocks is more complicated task. Easiest way to do it is to save the reference to the IDisposable link and dispose it if you don't need it anymore.
Other option is to link the blocks with new DataflowLinkOptions { MaxMessages = N }, but you need to know the exact number of messages to be delivered with a given link.
Also there is a neat option as Encapsulate method for wrapping the link between two blocks.

So, as far as you can see, there are a lot of opportunities to create some flow inside your app, but you have to define the rules by yourself. TPL Dataflow is an development tool, not the architecture pattern.

Upvotes: 1

Related Questions