Created
December 1, 2010 11:34
-
-
Save ToJans/723376 to your computer and use it in GitHub Desktop.
SoWhat.cs - An example of a possible .Net web framework looking like Sinatra
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
| // SoWhat.cs - An example of a possible .Net web framework looking like Sinatra | |
| // tojans@twitter | |
| using System; | |
| using System.Collections.Generic; | |
| public class ContactController | |
| { | |
| public class Contact | |
| { | |
| public string Name; | |
| public string Phone; | |
| public bool IsProspect; | |
| } | |
| public ContactController(ISoWhat sowhat) | |
| { | |
| sowhat.Handle.Get["/contacts"].With = ()=> { | |
| return "A list of all the contacts"; | |
| }; | |
| sowhat.Handle.Get["/contacts/{id}"].As<int>().With = id => { | |
| return "A single contact with id"+id.ToString(); | |
| }; | |
| sowhat.Handle.Get["/contacts/hello/{lastname},{firstname}"].As(new {Firstname="",LastName=""}).With = pars => { | |
| return string.Format("Hello {0} {1} !",pars.Firstname,pars.LastName); | |
| }; | |
| sowhat.Handle.Post["/contacts"].As<Contact>().With = c => { | |
| // do something with contact c | |
| return sowhat.Redirect("/contacts"); | |
| }; | |
| sowhat.Handle.Post["/contacts/image"].As<IHttpRequest>().With = req => | |
| { | |
| foreach (var key in req.Files.Keys) | |
| { | |
| // Do something with file streams here | |
| } | |
| return sowhat.Redirect("/contacts"); | |
| }; | |
| sowhat.Handle.With = () => { | |
| return sowhat.HttpStatus(404); | |
| }; | |
| } | |
| } | |
| public interface IHttpRequest | |
| { | |
| IDictionary<string,System.IO.Stream> Files {get;} | |
| } | |
| public interface IFilter | |
| { | |
| bool IsMatch(IHttpRequest HttpRequest); | |
| IFilter Request(Predicate<IHttpRequest> pred); | |
| IFilter this[string route] {get;} | |
| IFilter Get {get;} | |
| IFilter Put {get;} | |
| IFilter Post {get;} | |
| IFilter Delete {get;} | |
| Func<object> With {set;} | |
| IMatchMethod<T> As<T>(T instance); | |
| IMatchMethod<T> As<T>(); | |
| } | |
| public interface IMatchMethod<T> | |
| { | |
| Func<T,object> With {set;} | |
| } | |
| public interface ISoWhat | |
| { | |
| IFilter Handle {get;} | |
| void RegisterMapper<T>(Func<IHttpRequest,T> conv); | |
| object Redirect(string url); | |
| object HttpStatus(int code); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment