Monday, August 4, 2008

Hands- On with Astoria

The last blog familiarized you with the Astoria Service. As promised this blog would take you through concepts like creating an EDM, creating ADO.NET Data Service, consuming an ADO.NET Data Service and also helping to understand LINQ to SQL and XML.

Before Starting there are some Software prerequisites to be born in mind

· Use VS 2008 Express Edition or VS 2008 RTM Version
· ASP.NET 3.5 Extensions Preview
· ADO.NET Entity Framework Setup
· ADO.NET Entity Framework Tools
· SQL Server 2005 with a table TblCategory(aCategoryId, tCategoryName)


Creating ADO.NET Entity Data Model

The first step to go ahead would be creating An Entity Data Model for the service to be created. To go ahead with it

1. Create an empty website in VS and right click on the project name and select ADO.NET Entity Data Model.


2. A wizard prompts to create the EDM for the Database you require and also creates the Entities and context objects for the EDM created.


3. At the end of it you would find the .edmx file (having the table associations and mappings diagrammatically) and an auto generated logic file with same name as given to the EDM. This contains all the mapping and associations. This also adds the configuration to the .config file.





4. You can see the mapping by double clicking on the .edmx file in explorer.

Creating ADO.NET Data Service

We would now use the same EDM created above. ADO.NET Data Service which can be added by right clicking on the project name and selecting ADO.NET Data Service. Like WCF service it adds the logic file accompanied by the .svc file to the explorer.






Click on the WebDataService.cs ( according to the name given by you) and you see the something like below:

public class WebDataService : WebDataService
{
public static void InitializeService(IWebDataServiceConfiguration config)
{
// gives read permissions
config.SetResourceContainerAccessRule
("*", ResourceContainerRights.AllRead);

}
}

The Entity name( in this case have named it as TravelPortalEntities) forms one point of entry that associates this services to that particular model where in further querying as well as can use the stored procedures and views available in the EDM( provided you have selected it ).

Now you can view the .svc file you would be able to see the URL something like this
http://localhost:1028/adoservice/WebDataService.svc/

You can now query the above URL like
http://localhost:1028/adoservice/WebDataService.svc/tblCategory(1)
this gives the value from the table tblCategory with CategoryID 1




Consuming ADO.NET Data Service (Astoria)

We are now going to connect to the data service created above using Ajax.
Open an empty project and add DataService script class to it. This DataService class helps to execute data operations of a DataService we pointing do it. To create an instance of the DataService class

Var travelService= new Sys.Data.DataService(“TravelPortal.svc”);

TravelPoratl.svc is the name I gave to create the above service.
After you create the above instance you can all the query method to retrieve data from the data service.

Eg: travelService.query(“TblCategory”, cbSuccess, cbFailure);



This method references cbSuccess as the succeeded callback function and cbFailure as failed callback function, so accordingly you can frame both the references. You can also query with set to specific functions.


Hope this blogs helps you try on with Astoria.

ADO .NET Data Services for Web aka Astoria :

ADO .NET Data Services for Web aka Astoria :

The present era of web applications are built on technologies like Ajax and Silverlight enhancing the user experience. This has resulted in an obvious separation of presentation (UI) and data itself.

ADO .NET Data Services is a specialized WCF service that contains combinations of patterns and libraries that can be consumed by rich web clients there by keeping the separation between UI and Data intact. One question that would strike us is why do we need Astoria? Doesn’t WCF service also keep the separations intact? What more can Astoria give to a developer? Answering the above questions, as told Astoria is a specialized WCF service and the data is exposed as URI’s that points to data and is represented by simple formats like JSON and ATOM. This URI formats allows for simple queries to be formulated also has grater support for manipulating the presentation of the results like sorting, paging and expanding related resources. Also provides mechanism to point to every resource or member of a resource in the system.

The support of ADO.NET Data Services is provided in the ASP.NET 3.5 Extensions Preview which is a new preview of new functionality being added to ASP.NET 3.5 and ADO.NET. This preview includes ASP.NET MVC, ASP.NET MVC, ASP.NET Dynamic Data, ASP.NET controls for Silverlight, ADO.NET Data Services, Entity Framework runtime, and new features for ASP.NET AJAX. To know more about the other contents included in this preview version you can refer this link http://www.asp.net/downloads/3.5-extensions/.

To make data available to loosely coupled systems, we need to use a protocol that defines the interaction between the service and the client using it that too in an understandable format. The protocol used to leverage the most of Astoria is the HTTP protocol with JSON and APP/ATOM formats. Many of the APP and Atom concepts map well with the Astoria concept of exposing data. Also does HTTP gels with APP for manipulating data( adding, deleting, updating) which is the default protocol used in Astoria. And if we speak about JSON, it makes it easy to consume the results of services Js environments like web browsers.

ADO.NET Data Services consume a conceptual model representing the entities and its associations that the applications is likely to expose and exposes them as HTTP resource identified by a URI. The conceptual model is defined using Entity Data Model (EDM) proved by Entity Framework. While modeling this data you could analyze a scenarios, that is you are exposing the conceptual view of the data stored in the database as the Astoria well integrates with the Entity Framework.

To know more about the Entity framework along with the Entity Data Model(EDM) you could go through this reference link :
http://msdn2.microsoft.com/en-us/library/aa697427(VS.80).aspx.


Now the questions that would come to your mind would be How to create An Entity Data Model? How to create an ADO.NET Data Service? How To consume the same ? Etc….. Our next Blog would talk about this in detail…….