Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An initial (not great) example for asynchronous pipeline code. See blog post for more info: https://jermdavis.wordpress.com/2021/06/07/pipelines-and-async/ BUT you probably want this gist instead, because it's the (hopefully better) v2: https://gist.github.com/jermdavis/49ecd692a16b10899eb2ee2b50770499
async Task Main()
{
var pipeline = new ExampleAsyncPipeline();
var uri = new Uri("https://news.bbc.co.uk/");
var tempFile = await pipeline.ProcessAsync(uri);
Console.WriteLine($"{uri} saved to {tempFile}");
}
public interface IAsyncPipelineStep<TIn, TOut>
{
Task<TOut> ProcessAsync(Task<TIn> Input);
}
public abstract class AsyncPipeline<TIn, TOut> : IAsyncPipelineStep<TIn, TOut>
{
public Func<Task<TIn>, Task<TOut>> _pipelineSteps { get; protected set; }
public Task<TOut> ProcessAsync(Task<TIn> Input)
{
return _pipelineSteps(Input);
}
public Task<TOut> ProcessAsync(TIn Input)
{
var inputTask = Task.FromResult(Input);
return _pipelineSteps(inputTask);
}
}
public static class AsyncPipelineStepExtensions
{
public static Task<TOut> Step<TIn, TOut>(this Task<TIn> Input, IAsyncPipelineStep<TIn, TOut> Step)
{
return Step.ProcessAsync(Input);
}
}
public class HttpFetchAsyncStep : IAsyncPipelineStep<Uri, string>
{
private static readonly HttpClient _client = new HttpClient();
public async Task<string> ProcessAsync(Task<Uri> Input)
{
var uri = await Input;
return await _client.GetStringAsync(uri);
}
}
public class DiskWriteAsyncStep : IAsyncPipelineStep<string, string>
{
public async Task<string> ProcessAsync(Task<string> Input)
{
var data = await Input;
var fileName = System.IO.Path.GetTempFileName();
await System.IO.File.WriteAllTextAsync(fileName, data);
return fileName;
}
}
public class ExampleAsyncPipeline : AsyncPipeline<Uri, string>
{
public ExampleAsyncPipeline()
{
_pipelineSteps = input => input
.Step(new HttpFetchAsyncStep())
.Step(new DiskWriteAsyncStep());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment