Astoria DataSource Extender

Download Source and Example :

Most of you must have heard about Astoria. Read More about it here , if you haven't already :
Microsoft Codename "Astoria".
The Astoria Team Blog :

It's a "Database in the cloud", your db is hosted on the Web and you access it via REST -style URIs.

I have written a ControlExtender that helps one retrieve data and post data to a db on Astoria using JavaScript.
The Extender encapsulates its own Server-Side handler so that you don't have to write a Server-Side proxy to
avoid Making Cross-Domain Requests from the Client browser.

The way it would work is :
Client Browser =====> Server-Side handler ===> Astoria Servers

When the Client Script tries to download the data from the Astoria Servers, the following process is followed

  1. Client Script requests a certain page ( dataDownloader.astoria) on the same server as the website with all the Astoria Query related parameters as headers.
  2. The *.astoria extension is mapped to a handler contained in the Extender Control.
  3. Once the handler receives the request , it makes the call to the actual Astoria Servers and passes on the error/Data back to the requesting Client .
  4. The Control notifies any registered event handlers that the Data is now ready for use by the Script

What does the Extender NOT do ?

The Extender does NOT Support the following Scenarios

  1. Data Manipulation such as POSTING and Editing Data.
  2. Bind the Returned Data to HTML controls , this would have to be done manually.

 

Events Supported by the Extender:

Event Name Raised When Event Args Usage
dataReady When the Data is downloaded and available for use by the client application EventArgs contains the item( the Control itself) and the Data that the Client application can use See Example below
dataDownloadBegins When the Data download starts from the Astoria Servers EventArgs.Empty See Example below
dataDownloadEnds When the Data download Ends from the Astoria Servers . The Data may/may not be ready for use by the Client Application EventArgs.Empty See Example below
dataDownloadError When the Data download from the Astoria Servers results in an error AstoriaExtenders.DataDownloadErrorEventArgsContains the Error information See Example below

 

Subscribing to the Events of the Astoria Data Source Extender :

The BehaviorID of the Extender added on the page is : "asBehavior"

 //Subscribing to the dataReady event
$find("asBehavior").add_dataReady( dataReadyHandler );
//Subscribing to the dataDownloadBegins event
$find("asBehavior").add_dataDownloadBegins(<functionName>);
//Subscribing to the dataDownloadEnds event
$find("asBehavior").add_dataDownloadEnds(<functionName>);
//Subscribing to the dataDownloadError event
$find("asBehavior").add_dataDownloadError(dwError);
 //The dataReady Event handler
function dataReadyHandler(sender,eventArgs) {
   alert("The Data returned is :: "+eventArgs.get_data());
}
 //The dataDownloadError Event handler
function dwError(sender,eventArgs) {
    Sys.Debug.trace("error Code ::"+eventArgs.get_error().ErrorCode);
    Sys.Debug.trace("error Message ::"+eventArgs.get_error().ErrorMessage);
}
  
 Attributes Supported by the Extender
Attribute Name Description Usage Default
PageSize The Number of Records to show in one page See Example below 5
StartPageIndex The Initial page of Data returned See Example below 0
Query The Actual Astoria Query to return Data to the Client See Example below None
ExpandRecord The Sub-record which has to also expanded along with the parent Rows. See Example below None
DatabaseURI The Astoria URI to reach the Database See Example below None
DataFormat The Format in which the Client Requires data : json/xml/rdf See Example below json

Usage Examples

 $find("asBehavior").get_currentPageIndex();
$find("asBehavior").set_currentPageIndex(2);
 //Retrieve the Table 'Categories' from the 'NorthWind' Database
$find("asBehavior").set_dbURI("https://astoria.sandbox.live.com/northwind/northwind.rse");
$find("asBehavior").set_query("/Categories");
 $find("asBehavior").set_dataFormat("json");
$find("asBehavior").set_pageSize(10);
 Methods Supported by the Extender
Method Name Description Usage
DataBind Does what it says : it Downloads the data based on the parameters already specified See Example Below

Usage Examples

Bind the data to the client :

 $find("asBehavior").DataBind()

Move to a Specific page of data :

 //Move to the 3rd page
$find("asBehavior").set_currentPageIndex( 2 );
$find("asBehavior").DataBind();

Move to the Next page

 $find("asBehavior").set_currentPageIndex( 
               $find("asBehavior").get_currentPageIndex() + 1  
               );
$find("asBehavior").DataBind();

Move to the Previous page

 $find("asBehavior").set_currentPageIndex( 
                   $find("asBehavior").get_currentPageIndex() - 1  
                    );
$find("asBehavior").DataBind();.csharpcode, .csharpcode pre
{
 font-size: small;
   color: black;
   font-family: consolas, "Courier New", courier, monospace;
   background-color: #ffffff;
  /*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
   background-color: #f4f4f4;
  width: 100%;
    margin: 0em;
}
.csharpcode .lnum { color: #606060; }
 Markup Way of doing the above 
 <Raj:AstoriaDSExtender runat="server" ID="asbehaviorPnl" 
BehaviorID="asBehavior"
TargetControlID="btnAstoria" 
DatabaseURI="https://astoria.sandbox.live.com/northwind/northwind.rse"
DataFormat="json" StartPageIndex="0" PageSize="1" 
Query="/Categories" ExpandRecord="Products"
OnClientDataDownloadError="dwError" 
OnClientDataReady="endDownload" 
ErrorPanelId="divContainsErrorInfo" />
 Setting up your project to use the Astoria Extender
Web.Config : 
1. Register the Handler for the Extender
Add the Below line to the <httpHandlers> Section of your web.config.
<add path="*.astoria" verb="*" type="AstoriaExtenders.AstoriaHandler"/>
 2. The Tag Prefix for the Extenders
Add this line to the <controls> Section of the <Pages> Section in web.config
<add tagPrefix="Raj" namespace="AstoriaExtenders" assembly="AstoriaExtenders"/>
 3. Set up the proxy so that the Web Server can talk to the Astoria Servers.
<system.net>
    <defaultProxy>
      <proxy usesystemdefault="False" proxyaddress=https://proxyServerName bypassonlocal="True"/>
    </defaultProxy>
</system.net>