I'm trying to model series of "phases" C#, where each phase has an input and output, and where they can be chained together, with the output of phase 1 becoming the input of phase 2. I thought it might look something like this, but I didn't know what to use in place of the ???.
public class Phase<TInput, TOutput>
{
private Phase<???, TInput> prerequisite;
public Phase(Phase<???, TInput> prereq, Func<TInput, TOutput> work)
{
/* ... */
}
}
What I'm ultimately trying to do is chain together a series of steps like this (where the "Add" methods are just convenience methods to help me create the phases):
var p1 = AddInitialPhase(() =>
{
int a = /* some initial result */;
return a;
});
var p2 = AddPhase(p1, (result) =>
{
string b = /* do something with result from p1 */
return b;
});
var p3 = AddPhase(p2, (result) =>
{
/* etc... */
});
Is there any nice type-safe way to do this with generics? Or does anyone have design alternatives I should consider? I can imagine using reflection to do the chaining, or just using objects for the inputs and outputs, but I was hoping there was a better way.