Skip to content

Instantly share code, notes, and snippets.

@mhoyer
Created March 20, 2016 12:14
Show Gist options
  • Save mhoyer/016b5d88ddaf8d9c034a to your computer and use it in GitHub Desktop.
Save mhoyer/016b5d88ddaf8d9c034a to your computer and use it in GitHub Desktop.
A basic async approach for middleware (dolls) using tasks and LINQ aggregate
using System.Diagnostics;
using System.Linq;
using NUnit.Framework;
using System;
using System.Threading.Tasks;
namespace AsyncDollsAggregator
{
[TestFixture]
public class Test
{
[Test]
public async Task RunAsyncDolls()
{
Func<Task> done = () =>
{
Console.WriteLine("Done\n\n{0}", new StackTrace());
return Task.CompletedTask;
};
await AsyncInvoke(
AsyncDecrypt,
AsyncUnzip,
AsyncAuthorize,
a => done());
}
Task AsyncInvoke(params Func<Func<Task>, Task>[] actions)
{
Func<Task> seed = () => Task.CompletedTask;
return actions
.Reverse()
.Aggregate(seed, (next, action) => () => action(next))();
}
Task AsyncUnzip(Func<Task> next)
{
Console.WriteLine("Unzip");
return next();
}
Task AsyncDecrypt(Func<Task> next)
{
Console.WriteLine("Decrypt");
return next();
}
Task AsyncAuthorize(Func<Task> next)
{
Console.WriteLine("Authorize");
return next();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment