-
-
Save csainty/4433314 to your computer and use it in GitHub Desktop.
This file contains 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; | |
using System.Collections.Generic; | |
using System.Net; | |
using System.Threading.Tasks; | |
using Xunit; | |
public class Fixture | |
{ | |
[Fact] | |
public void Should_FactMethodName() | |
{ | |
var module = new Class1(); | |
var result = module.routes["/"].Invoke(new object()); | |
Assert.Equal("non async", result); | |
} | |
[Fact] | |
public async Task Should_work_async() | |
{ | |
var module = new Class1(); | |
var result = await module.routes["/stuff"].Invoke(new object()); | |
Assert.Equal("async", result); | |
} | |
} | |
public class Class1 : Module | |
{ | |
public Class1() | |
{ | |
Get["/"] = x => | |
{ | |
return "non async"; | |
}; | |
Get["/stuff"] = Stuff; | |
} | |
public async Task<dynamic> Stuff(dynamic _) | |
{ | |
await Task.Delay(100); | |
return "async"; | |
} | |
} | |
public class Module | |
{ | |
public readonly IDictionary<string, Func<dynamic, dynamic>> routes = new Dictionary<string, Func<dynamic, dynamic>>(); | |
public Builder Get | |
{ | |
get { return new Builder(this); } | |
} | |
public class Builder | |
{ | |
private readonly Module module; | |
public Builder(Module module) | |
{ | |
this.module = module; | |
} | |
public Func<dynamic, dynamic> this[string path] | |
{ | |
set { this.module.routes.Add(path, value); } | |
} | |
//public Func<dynamic, Task<dynamic>> this[string path] | |
//{ | |
// set { this.module.routes.Add(path, value); } | |
//} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment