TaskChain - slavikdev/sharp-patterns GitHub Wiki

The chain which executes tasks in the order they were passed. This class implements the chain of responsibility pattern which is used to split a process to smaller parts. Tasks passed to this chain return result of some type.

// All tasks must implement IChainedTask interface
// with common return type.
// Of course instances of the tasks 
// can be of different classes.
IChainedTask<int>[] tasks = {
  new Incrementor(),
  new Incrementor(),
  new Incrementor()
};

var chain = new TaskChain<int>( 
  // Initial value which will be passed to the first task in chain.
  0, 
  tasks 
);

// Result contains the result of the last task in chain.
int result = chain.Run();

It’s also possible to create a chain like this:

new TaskChain<TaskResult>( 
  new TaskResult( 0 ),
  new InitialResult(), 
  new OneTask(),
  new AnotherTask(),
  new YetAnotherTask(),
  new FinalTask()
);
⚠️ **GitHub.com Fallback** ⚠️