I'm thinking about a chain, that will allow me to perform a set of transformations with data type changing from transformation to transformation. So far I've got something like this:
public abstract class TransformationStep<T, TY>
{
public abstract TY Execute(T input);
}
public class Times2<TY> : TransformationStep<int, TY>
{
public Times2(TransformationStep<int, TY> next)
{
Next = next;
}
public TransformationStep<int, TY> Next { get; set; }
public override TY Execute(int input)
{
var res = input * 2;
return Next.Execute(res);
}
}
public class ToString<TY> : TransformationStep<int, TY>
{
public ToString(TransformationStep<string, TY> next)
{
Next = next;
}
public TransformationStep<string, TY> Next { get; }
public override TY Execute(int input)
{
var res = input + "!!!";
return Next.Execute(res);
}
}
The only problem I see is the end chain type, where I can't convert T to TY.
public class End<T, TY> : TransformationStep<T, TY>
{
public override TY Execute(T input)
{
return input;
}
}
Do you have any solution? Also what do you think about this design and do you know any good materials on stuff like that?
It looks like you want transformations:
And if you want to just return input type, then it is possible to create another method with return type:
Dataflow
If you want to connect chains of data into pipeline or graph, then you can use TransformBlock.
What is about Chain of Responsibility pattern?
As wiki says about "Chain of Responsibility pattern":
Your code looks similar, however, code and goal of chain responsibility pattern is slightly different. It does not make transformations, it gives object to the next processing object in the chain.
So one of the variations of code of chain of the responsibility pattern can look like this:
An abstraction of desired behaviour of chain of the responsibility pattern:
And let us imagine that we are publishing house and we want that each property of article should be validated. So, concrete implemetations of handling article can look like this:
And
ArticleProcessorwould look like this:And it can be run like this: