Skip to content

Instantly share code, notes, and snippets.

@kiwidev
Forked from thecodejunkie/gist:4429702
Last active December 10, 2015 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiwidev/4433287 to your computer and use it in GitHub Desktop.
Save kiwidev/4433287 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Net.Http;
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());
var i = 10;
}
}
public class Class1 : Module
{
public Class1()
{
Get["/"] = x =>
{
return "non async";
};
Get["/stuff"] = Async(async x =>
{
var client = new HttpClient();
var result = await client.GetAsync("http://www.nancyfx.org");
var content = await result.Content.ReadAsStringAsync();
return content;
});
}
}
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); }
}
// Helper to ensure that the async lambda is treated as a Task<dynamic>
public static Func<dynamic, dynamic> Async(Func<dynamic, Task<dynamic>> function)
{
return function;
}
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); }
}
}
}
@kiwidev
Copy link
Author

kiwidev commented Jan 2, 2013

Added an Async static method to allow for casting of async lambda to Func<dynamic, dynamic>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment