34
loading...
This website collects cookies to deliver better user experience
public interface IPipeStep<TPipeModel>
{
Task Execute(TPipeModel pipeModel);
}
IPipeService
with method definition to set up pipeline and IPipeServiceExecution
to define method for pipe execution:public interface IPipeService<TPipeModel> : IPipeServiceExecution<TPipeModel>
{
IPipeService<TPipeModel> Add(Func<IPipeStep<TPipeModel>> pipeStep);
}
public interface IPipeServiceExecution<TPipeModel>
{
Task ExecuteAsync(TPipeModel pipeModel);
}
public class PipeService<TPipeModel> : IPipeService<TPipeModel>
{
private readonly IList<Func<IPipeStep<TPipeModel>>> _pipeSteps;
public PipeService()
{
_pipeSteps = new List<Func<IPipeStep<TPipeModel>>>();
}
public IPipeService<TPipeModel> Add(Func<IPipeStep<TPipeModel>> pipeStep)
{
_pipeSteps.Add(pipeStep);
return this;
}
public async Task ExecuteAsync(TPipeModel pipeModel)
{
foreach (var pipeStep in _pipeSteps)
{
await pipeStep.Invoke().ExecuteAsync(pipeModel);
}
}
}
IPipeServiceExecution
should be returned. Isn’t it a great place for factory? Indeed, it is!CreatePipe
method returns IPipeServiceExecution
interface, so the only action that can be performed when pipe is returned from factory is its execution.public interface IPipeFactory<TPipeModel>
{
IPipeServiceExecution<TPipeModel> CreatePipe();
}
IPipeService
interface to have a possibility to add error handling steps which will be executed in case pipe breaks:public interface IPipeService<TPipeModel> : IPipeServiceExecution<TPipeModel>
{
IPipeService<TPipeModel> Add(Func<IPipeStep<TPipeModel>> pipeStep);
IPipeService<TPipeModel> AddErrorStep(Func<IPipeStep<TPipeModel>> errorStep);
}