Skip to content

Instantly share code, notes, and snippets.

@jjvdangelo
Created June 27, 2013 15:47
Show Gist options
  • Save jjvdangelo/5877592 to your computer and use it in GitHub Desktop.
Save jjvdangelo/5877592 to your computer and use it in GitHub Desktop.
Example of what I'm trying to achieve in Simple.Web using behaviors.
namespace MyCompany.Product
{
// I tend to utilize immutable value objects in my domain and am
// attempting to, basically, inject them into my handlers via
// a behavior--tying into the Simple.Web pipeline.
public class Foo
{
private readonly string _value;
public string Value { get { return _value; } }
public Foo(string value)
{
_value = value;
}
}
[RequestBehavior(typeof(SetFoo))]
public interface IRequireFoo
{
Foo MyFoo { set; }
}
public static SetFoo
{
public void Impl(IRequireFoo handler, IContext context)
{
// Given a handler, I would like to extract out the values
// that were pulled out of the UriTemplate
var uriValues = // somehow get the key-value pairs extracted from the URL
// that match the "matched" UriTempalate.
// And I'm looking to do something like:
handler.FooId = new Foo(uriValues["fooId"]);
}
}
// Given the UriTemplate on this class and a request to
// /foos/12345/bar/54321, I am looking to extract
// something like:
// UriKVPs = {
// { "fooId", "12345" },
// { "barValue", "54321" }
// }
[UriTemplate("/foos/{fooId}/bar/{barValue}")]
public class FooHandler : IGet, IRequireFoo, IOutput<string>
{
public Foo FooId { get; set; }
public string Output { get; set; }
public string BarValue { get; set; }
public Status Get()
{
Output = FoodId.Value;
return 200;
}
}
// I am trying to avoid something like:
public interface IRequireFoo2
{
string FooValue { set; } // Simple.Web will bind to this
Foo FooId { set; } // And then I can use the string to create the Foo
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment