Martin: A Lightweight REST Framework Inspired by Sinatra and MVC
Those of you who work in the Rails community will know something about Sinatra. It's a lightweight web framework for building REST services in Ruby. I'm not a Ruby programmer (I know enough to be dangerous) but I really like the idea of Sinatra.
The basic premise of Sinatra is that every method defines a route, and a route (URL + Verb) invokes a single method. Ruby, being a dynamic language, makes this extremely easy. Like I said I'm no Ruby guru but the built-in introspection of the language makes it easier to build a DSL that can describe these route methods.
Martin, isn't at all a port of Sinatra. Rather it's inspired by the lightweight nature of Sinatra and it's general pattern. Martin relies on the new System.Web.Routing namespace in .NET 3.5 SP1 and will eventually ship with a DSL that will encapsulate defining your RouteTable. Because it doesn't have any external dependencies and really doesn't need any templates it can plug into any ASP.NET or ASP.NET MVC project and live along side just fine*.
In Martin, the first step to creating a new action is defining it with the Config API. At this point it's excruciatingly simple.
In Global.asax:
What this does is maps the URL: http://domain.com/Show/CurrentDate to the DateAction.Get() method. Behind the scenes there is a factory class that's constructing instances of DateAction, calling the Get() method, and then responding with the result to the request pipeline.
The DateAction class looks like this:
There are four methods on ActionBase that can be overridden: Get(), Post(), Put(), and Delete(). Each method (obviously) corresponds to an HTTP Verb and only requests using that verb will invoke the appropriate method. So, the same URL pattern can be mapped to two different methods provided that the request uses different verbs.
The ActionBase class provides a way to get access to any RouteData or Request items that might be part of the request. Using the ValueOf(String key) method you can find for example an ID that's tacked on to the URL or a piece of data from the querystring or form collection.
~~
For now the project is in a very early stage, no real sample, no real documentation. However, it's so simple that reading through the handful of classes that make up the project is almost all the documentation you'll need. If you get a chance, poke around and let me know what you think...if you want to help make it better just let me know and I'll get you commit rights to the Google Code repo.
Source at: http://code.google.com/p/martin-framework/
* In the case of integration with ASP.NET MVC the routes defined by Martin and those your application might need could conflict so care must be taken to insure there are no collisions.
protected void Application_Start(object sender, EventArgs e) {
Config.AddAction("Show/CurrentDate");
}public class DateAction : ActionBase {
public override IResponse Get() {
return Text(string.Format("The current date is {0}.", DateTime.Now));
}
}