Introduction
After reading the title of this article you were probably brought here by curiosity “what is Astoria?”
Astoria is a new technology from Microsoft that builds on an established technology Windows
Communication Foundation (WCF) and a soon to be de facto data access layer (DAL)
technology called Entity Data Model (EDM) which is due for release some time after the Orcas line of
products.
Astoria got released at Mix ’07 in the form of a Community Technology Preview (CTP); this is the
first chance that we have had to look at Astoria.
Put simply Astoria uses familiar standards based mechanisms to access conceptual data defined in an
EDM. We do this by using standard HTTP verbs like GET, POST,
PUT, etc.
So why the fuss? Astoria is something that rich internet applications (RIA’s) can really take
advantage of. By firing off asynchronous requests to a server using a uniform resource identifier (URI) from your chose technology you can really take advantage of Astoria web data services.
I must admit Astoria is one of those technologies that genuinely get’ me excited – anyone who has
built an ASP.NET AJAX application and has fired requests off to web service methods which are fairly
static in their composition will really like this technology.
Creating an Astoria Data Service
I mentioned earlier that Astoria is built on two technologies namely WCF, and
EDM. It should come as no surprise then that we need to create each – but we do have some aid
provided in the form of new item templates in Visual Studio Orcas Beta 1.
Note: Although the
EDM wizard comes with Visual Studio Orcas Beta 1, the
Astoria
CTP does not and you will have to download this from the
Microsoft downloads site.
My intention is to not make this a tutorial but rather an informal chat about Astoria and as such I
will talk lightly about the steps involved in creating an Astoria data service but I will not go
into the explicit steps required.
First thing is first, we need to create an EDM model for our database I have chosen to use the
popular Northwind database. This EDM will be used when creating our Astoria data
service.
The second step to getting an Astoria data service up and running is to actually add a “Web Data
Service” item to your web application project.
Figure 1-1: Adding a Web Data Service to your application
With the web data service added you need to go into the code behind for that service and derive from the
XxxEntities type which the EDM wizard generated for you.
Note: The XxxEntities type is defined in a namespace XxxModel where
Xxx in each case is normally the name of the target database, e.g. the Northwind namespace generated
is NorthwindModel.
I decided to host my Astoria services using IIS 7 however you can do this using IIS 6, but I haven’t
tested it.
When you browse to your service you will see a container XML element called XxxEntities which
contains all the entity children that the EDM wizard generated for us. Each entity maps 1..1 with
physical tables in your database unless you chose to customize the mappings.
Note: The service endpoint has a file extension of .svc to indicate it is a WCF service.
Figure 1-2: Viewing the service endpoint
You can see that in Figure 1-2 I have several entities available to me from my Astoria web data
service - Suppliers, Products, Orders, etc...
Consuming the service using the Microsoft AJAX Library
In this article I am going to use ASP.NET AJAX to query the Astoria web data service,
you can however use pretty much any technology – I myself have used ASP.NET AJAX, several windows
applications as well as WPF (and it’s binding syntax) to use Astoria, each with minimal
noise.
Just before we see some code I would like to point out that ASP.NET Futures does provide support for consuming
Astoria web data services – I have chosen not to use this however to reach a wider audience, after
all everyone is still learning ASP.NET AJAX 1.0! The API introduced into ASP.NET Futures really
isn’t that much of a big deal; it just removes a few steps that are a little “dirty” as you will see in the
following code.
With ASP.NET AJAX we are familiar with sending some async request to a web server and receiving some
data back – this is a paradigm established with AJAX applications where we separate the
presentation, behaviour and data. Commonly the presentation is HTML, the behaviour is
JavaScript which communicates with the server on the clients behalf and the data is served up in
either XML or JSON.
Astoria web data services support three types of data exchange format including:
- XML
- JSON
- RDF+XML
For this article we will, of course, be using JSON. Using JSON allows us to interact
with objects after eval’ing the JSON string passed to us from the server.
I think for the first query let’s just go ahead and retrieve all of the customers.
Listing 1-1: Retrieving all of the customers
Note: There is a class that comes with the Astoria samples that provides a class and series
of methods to create a more encapsulated approach to calling an Astoria web data service.
Figure 1-3: Running Listing 1-1.
It’s not my intention to go through the various filtering syntax in this article or all of the features provided
by Astoria web data services like adding, updating and deleting data.
Before I wrap this up let’s take a lot at some queries where we use the URI to further filter the results.
Listing 1-2: Ordering the results by ContactName
Listing 1-3: Selecting the top 10 results from Listing 1-2.
Summary
Astoria is set to provide RIA developers with a richer way of interacting with data on the server.
The syntax is easy and there is a more structure API on the way for ASP.NET AJAX in ASP.NET Futures
(the Mix.DataService JavaScript namespace is included with the Astoria CTP samples).
Go and download the CTP bits and give it a go!
Please login to rate or to leave a comment.