Created
March 20, 2016 12:14
-
-
Save mhoyer/016b5d88ddaf8d9c034a to your computer and use it in GitHub Desktop.
A basic async approach for middleware (dolls) using tasks and LINQ aggregate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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