Thursday, October 4, 2007

Getting started with Astoria

Few weeks ago, the CTP refresh for VS 2008 Beta 2 of Astoria was released. Astoria is a really cool project based on .NET 3.0 (WFC), Entity Framework to provide pure HTTP access to SQL Server data.In order to play with it, first of all you need to install the following:

After installing all we need, we can create out first project - this post will describe the steps you need in order to do that.

Create new Web Application Project:

1

After I did it, I need to add new ADO.NET Entity model. On the Project menu, click 'Add new item' and select the ADO.NET Entity Data Model.

2

Now, the wizard will start. Select: Generate model from database.

3

Next we'll save the connection info

4

I checked the option to add connection string to web.config and that's what I got there:

<add name="NorthwindEntities" connectionString="metadata=~/bin/NorthWind.csdl|~/bin/NorthWind.ssdl|~/bin/NorthWind.msl;
provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\sqlexpress;Initial Catalog=Northwind;
Integrated Security=True&quot;"
providerName="System.Data.EntityClient" />

The next step we select the objects we want. I choose Tables only.


5


What we got so far? .edmx file was added to our project, which is the schema.


6


Next step we should add and setup Astoria service. We'll do it from the Project menu --> Add new item.


7


New class was generated, it's not full cause we should select the type of the service.


8


I added the NorthwindModel.NorthwindEntities type. (NorthwindModel is the model I generated)


9


Now everything ready to compile...


After compilation, I can browse to the service I created. Look what I got:


10


The service describes the schema with the object I selected (Tables only).


How can I query the service? For example, if I want all the categories, I'll change in the address in my browser to: http://localhost:58889/NorthwindWebDataService.svc/Categories and I got all the categories:


(localhost:58889 is on my computer off course, change your host:port to match yours)


11


Amazing....


What about query it from asp.net app? 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. You can find it at the installation folder.


What if I want to query the service to get the customers order by contact name:


http://localhost:58889/NorthwindWebDataService.svc/Customers?$orderby=ContactName


and what if I want the top 10 records:


http://localhost:58889/NorthwindWebDataService.svc/Customers?$orderby=ContactName&$top=10


Conclusion


Astoria is set to provide the developers 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.


 


Technorati Tags: ,

No comments: