J’aborde OData dans différents articles, aussi pour éviter de me répéter dans chacune des introductions, voici un petit rappel.
OData est un protocole de manipulation de données basé sur http en REST. On peut filtrer, paginer, trier mais aussi modifier et supprimer ces données grâce à une simple URI (c’est à dire un “chemin web” comme ceux que vous saisissez dans la barre d’adresse de votre navigateur).
En simplifié:
Avantages et caractéristiques:
Microsoft WCF Data Services (anciennement Astoria) est le composant de .Net qui permet d’implémenter OData.
Un petit exemple sur un service OData http://odata.microsoftpdc.com/ODataSchedule.svc/ qui nous renseigne sur les évènements de la PDC 2010 :
La requête: http://odata.microsoftpdc.com/ODataSchedule.svc/ (vous pouvez la tester dans votre navigateur préféré)
Nous renvoie les éléments accessibles à la racine : collections de sessions, speakers, … :
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<service xml:base="http://odata.microsoftpdc.com/ODataSchedule.svc/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2007/app">
<workspace>
<atom:title>Default</atom:title>
<collection href="ScheduleOfEvents">
<atom:title>ScheduleOfEvents</atom:title>
</collection>
<collection href="Sessions">
<atom:title>Sessions</atom:title>
<collection href="Tracks">
<atom:title>Tracks</atom:title>
<collection href="TimeSlots">
<atom:title>TimeSlots</atom:title>
<collection href="Speakers">
<atom:title>Speakers</atom:title>
<collection href="Manifests">
<atom:title>Manifests</atom:title>
<collection href="Presenters">
<atom:title>Presenters</atom:title>
<collection href="Contents">
<atom:title>Contents</atom:title>
<collection href="RelatedSessions">
<atom:title>RelatedSessions</atom:title>
</workspace>
</service>
Pour filtrer à la source, il suffit de suffixer le nom du service par l’arborescence nous amenant à un élément ou une collection d’éléments et d’y ajouter (ou pas) une requête (OrderBy, Take, Skip, …) et son prédicat.
Dans notre exemple, nous souhaitons consulter les 2 premiers speakers triés par ordre alphabétique.
La requête devient: http://odata.microsoftpdc.com/ODataSchedule.svc/Speakers/?$orderby=FullName&$top=2 (plus d’info sur les requêtes ici http://www.odata.org/developers/protocols/uri-conventions#QueryStringOptions)
Et renvoie: Adam Wilson et Amit Chopra
<feed xml:base="http://odata.microsoftpdc.com/ODataSchedule.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Speakers</title>
<id>http://odata.microsoftpdc.com/ODataSchedule.svc/Speakers/</id>
<updated>2010-11-05T13:27:16Z</updated>
<link rel="self" title="Speakers" href="Speakers" />
<entry>
<id>http://odata.microsoftpdc.com/ODataSchedule.svc/Speakers(guid'7553a0ea-159f-452e-bb5e-a94edfb0cd9d')</id>
<title type="text" />
<author>
<name />
</author>
<link rel="edit" title="Speaker" href="Speakers(guid'7553a0ea-159f-452e-bb5e-a94edfb0cd9d')" />
<category term="Shared.Model.Speaker" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm.Guid">7553a0ea-159f-452e-bb5e-a94edfb0cd9d</d:Id>
<d:FullName>Adam Wilson</d:FullName>
<d:PhotoUrl m:null="true" />
<d:Bio m:null="true" />
</m:properties>
</content>
</entry>
<id>http://odata.microsoftpdc.com/ODataSchedule.svc/Speakers(guid'758b9c4f-5b12-4ef2-8cf9-5e3652f5dd3e')</id>
<link rel="edit" title="Speaker" href="Speakers(guid'758b9c4f-5b12-4ef2-8cf9-5e3652f5dd3e')" />
<d:Id m:type="Edm.Guid">758b9c4f-5b12-4ef2-8cf9-5e3652f5dd3e</d:Id>
<d:FullName>Amit Chopra</d:FullName>
<d:PhotoUrl>http://pdccms.istreamplanet.com/content/AmitChopra.jpg</d:PhotoUrl>
<d:Bio>Amit Chopra is a Senior Program Manager with the Visual Studio Test and Lab Team which is responsible for creating the Visual Studio 2010 Test Professional Product and the Lab Management Features. Amit has spent over 10 year in the Developer Division working on variety of IDE Features targeted towards Office Developers, Windows Phone Developers and most recently on features aimed to enhance Developer / Test collaboration in teams that are using Visual Studio Team Foundation Server as the core ALM Platform. Prior to working for Microsoft, Amit has worked for companies such as PricewaterhouseCoopers, Sun Microsystem, CANON and DEC in various capacities in the engineering and developer evangelism groups.</d:Bio>
</feed>
L’introduction à OData s’arrête ici.
Pour aller plus loin :