REST assured and AJAXED
some incomplete thoughts...
Its so interesting how we forget simple approaches to implementing stuff, particularly with the craziness of WS-* standards. Among all that Roy Fielding came up with paper around a proposed implementation approach called REST: Representational State Transfer
So lets say you want a page that manipulates date for example stock quotes obviously you want this data to be updated every 10 sec without the jerky browser behavior of refresh,
To build on top of that the data is the following
class Quote
{
string StockName;
decimal StockValue
}
probably the first thing that comes into your mind is wsBasicHTTP binding WCF service or ASMX and then add some Ajaxed control that does that. But think of it, the time to market for that is a bit long needless to say the irrelevant time consuming tasks of designing service interface
What Roy is saying is deferent, imagine this
A get request against http://localhost/Stocks/<stockname> will get you this XML
<stock name=”<name here>” value=”<stock value here>” />
A post request against http://localhost/Stocks/<stockname> with the <stock name=”<name here>” value=”<stock value here>” />
Will update the stock value (maybe from some backend system)
The approach is so simple and yet so powerful the part I love the most is you don’t have to work with custom standards commands like webDAV (LOCK/LIST/GET etc..)
More about it here http://www.xfront.com/REST-Web-Services.html
The part I hate the most about this approach and other Ajax (XML over HTTP) is data is represented to browser as XML and then you will have to write a lot of commands against (“MSXML.XMLDocument”) to read the data
What I am thinking of something relates to representing the data as JS objects yes those old good ones like
//JS CODE
Function Stock(sStockName, dStockValue)
{
this.StockName = sStockName;
this.StockValue = dStockValue;
}
So what I have written are some js serializer (probably HTTP Handler) that takes data classes and create JS objects out of them, then these data objects can be sent back and forth to REST handler.
So you would end up with 2 URL schemes one for the data which might look like
http://localhost/Users/{userid}
and another URL for the metadata which might look like
http://localhost/metadata/user
Here is a piece of pseudo code that illustrate that
Function getJSType{Generic}(Generic T)
Generate JS Function Header: “function Name(T)”
Foreeach member in Generic T
Generate: “this.(T.MemberName)” = “T.MemberName.Value”
Generate Js Function Ending..
End Function
I had an actual .NET code that done this, somehow I lost it :(
The previous code will generate the metadata, another handler could take care of generating the actual instance by returning JS code to the client instead of XML
Something like var c = new Customer(“fname”, “lName”);
The client JS helper might then embed this in browser by using document.write.
It just another idea I had in mind, might actually work out.