WCF Data Services, REST & OData

triangledriprockInternet and Web Development

Aug 7, 2012 (4 years and 10 months ago)

1,246 views

Christoph Pletz

Senior Consultant

Trivadis

AG

WCF Data Services,

REST
& OData

REST and
OData

overview

OData

features

Implementing
OData

clients

Implementing
OData

services

Agenda

Why data services?

B
reak data silos

D
ata sharing and integration

“ODBC for the web”

Big picture

protocol

producers

consumers

2006


Codename “Astoria”

2008


First release of “ADO.NET Data Services”

2009 ...

Renamed to “WCF Data Services”

OData

announced and released under
the

“Open
Specification
Promise”


2011


March
2011 CTP2 for .NET4 & SL4

History

Open Data Protocol

Design goals ...

Invent as little as possible

Very
low barrier of entry

OData is a RESTful protocol

OData

Resources are the center piece

resources have addresses (URIs)

no custom behavior

Uniform interface ...

fixed set of operations (GET, POST, PUT, DELETE)


REST

REST versus SOAP

SOAP

Operations, messages

WS
-
*
protocols

Routing, policy, reliable messaging, message
security, asynchrony, message correlation,
distributed transactions


OData

Builds on HTTP and AtomPub

Defines ...

XML + JSON data representation

Addressing scheme

Query operators

Metadata


www.odata.org
OData servers

Windows
Azure
Marketplace
DataMarket


IBM
WebSphere

eXtreme

Scale REST data
service

More at ...

http://
www.odata.org/producers



OData clients

Browsers


OData
Explorer

LINQPad


More at ...

http://
www.odata.org/consumers



REST and
OData

overview

OData

features

Implementing
OData

clients

Implementing
OData

services

Agenda

http://odata.netflix.com/Catalog
/

AtomPub service document, list of all collections

http://
odata.netflix.com/Catalog/Genres

all entries of a collection

http
://.../
Catalog/Genres('Adventures
')

one entry by PK

http
://.../
Catalog/Genres('Adventures')/
Name


one property as XML

http
://.../
Catalog/Genres('Adventures')/Name/$
value

only the value

OData navigation

http
://.../
Catalog/Genres('Adventures')/
Titles

related entities

http
://.../
Catalog/Genres('Adventures')/$
links/Titles

only the links

http
://.../
Catalog/Genres('Adventures')/Titles/$
count

count

OData

navigation

... /Titles?$filter=
AverageRating

gt

4

filters

... /Titles?$
orderby
=
AverageRating

desc
,
ReleaseYear

asc

sorting

.../
Titles?$select=
ShortName
,
ReleaseYear

projection

Paging support: $top, $skip, $
skiptoken
, $
inlinecount


OData

queries

http
://odata.netflix.com/Catalog/$
metadata

EF EDMX model


Metadata

All operators are text
to
prevent URL problems

not, and, or

mul
, div, mod, add, sub

eq
, ne,
lt
,
gt
, le,
ge

cast,
isof

Functions

endswith
,
startswith
,
substringof

tolower
,
toupper
, trim, substring,
concat

year, month, day, hour, minute, second

round, floor, ceiling

Operators and functions

LINQPad

OData

Explorer

REST and
OData

overview

OData

features

Implementing
OData

clients

Implementing
OData

services

Agenda

“Add Service Reference” in VS for .NET and SL

VisualStudio

OData

Visualizer Extension

install using VS Extension Manager


Generated proxy

Ajax


getJSON


$(document).ready(
function

() {


var

url

=
"http
://.../
ProductsDataService.svc
/Categories"
;


$.
getJSON
(
url
,
function

(result) {


var

table =
'<table><
thead
><
th
>Name</
th
>'

+


'<
th
>Description</
th
></
thead
><
tbody
>'
;




for

(
var

cat
in

result.d
)
{


...

REST and
OData

overview

OData

features

Implementing
OData

clients

Implementing
OData

services

Agenda

Service implementation

Data Services Runtime

HTTP
traffic

Entity Framework
Provider

Reflection
Provider

Custom
Provider

.NET Classes

[+ LINQ provider]

Entity
Framework

Database

Data
Source

Data
Source

Custom providers:

-

IDataServiceMetadataProvider

-

IDataServiceQueryProvider


Updates:

-

IDataServiceUpdateProvider


Special features:

-

IDataServiceStreamProvider

-

IDataServicePagingProvider

Start with an “Empty ASP.NET Application”

Add an “ADO.NET Entity Data Model


Add
a
“WCF Data Service”


WCF Data Services
with
EF

Tell
the data service which entity model to use

Set some security


WCF Data Services with EF

public

class

ProductsDataService

:
DataService
<
NorthwindEntities
>

{


public

static

void

InitializeService
(
DataServiceConfiguration

config
)


{


config.SetEntitySetAccessRule
(
"Categories"
,
EntitySetRights
.All
);


config.SetEntitySetAccessRule
(
"Products"
,
EntitySetRights
.All
);

Custom logic/security with
QueryInterceptor

QueryInterceptor

[
QueryInterceptor
(
"Categories"
)]

public

Expression
<
Func
<
Category
,
bool
>>
InterceptCategoryQuery
()

{


var

auth

=
HttpContext
.Current.User.Identity.IsAuthenticated
;


return

c =>
auth

||
c.IsPublic
;

}

Customize the returned AtomPub document by
adding attributes to the conceptual EF model

Atom feed customization

<
EntityType



Name
=
"
Category
"



xmlns:m
=
"
http
://.../
ado/2007/08/
dataservices
/metadata
"



m:HasStream
=
"
true
"
>


...


<
Property

Name
=
"
CategoryID
"

Type
=
"
Int32
"

...


<
Property

Name
=
"
CategoryName
"

Type
=
"
String
"

...


m:FC_TargetPath
=
"
SyndicationTitle
"



m:FC_KeepInContent
=
"
true
"

/>


...

Attributes ...

FC_KeepInContent
,
FC_NsPrefix
,
FC_NsUri
,
FC_SourcePath
,
FC_TargetPath

Special
FC_TargetPath

values ...

AuthorEmail
,
AuthorName
,
AuthorUri
,
ContributorEmail
,
ContributorName
,
ContributorUri
, Published, Rights,
Summary, Title, Updated


Atom feed customization

Mark with
WebGet
,
WebInvoke

attributes

Custom service operations

[
WebGet
]

public

IQueryable
<
Product
>
GetProductByCategory
(
string

name)

{


var

q =
from

p
in

CurrentDataSource.Products



where

p.Category.CategoryName

== name


select

p;


return

q;

}

ChangeInterceptors

Provide
access to each changed entity before the
update operations are performed on the
database

ProcessingPipeline

events

ProcessingChangeset
,
ProcessedChangese

Custom code before and after batch updates



Updates (server)

Implement a class
that supports the
IDataServiceStreamProvider

interface

The data
-
service must support
IServiceProvider


Stream provider

Implement a class that has
IQueriable

properties

Reflection provider

[
DataServiceKey
(
"Name"
)]

public

class

Person

{


public

string

Name {
get
;
set
; }


public

int

Age {
get
;
set
; }

}

public

class

PersonService

{


public

IQueryable
<
Person
> Persons


{


get

{
return

_
persons.AsQueryable
(); }


}


private

List
<
Person
> _persons =
new

List
<
Person
>();



...

}

OData

web site

http://odata.org


team
blog

http://blogs.msdn.com/astoriateam

msdn

“data” center

http://msdn.microsoft.com/data

Visualizer

In Visual Studio 2010


Tools


Extension Manager


Search


“open data protocol”


Resources

Summary

OData

provides an open and simple mechanism to
access data from any platform

Already wide support of
OData

not only in Microsoft
products

Best when the focus is on reading
data

For complex business logic in updates think about
combining it with WCF (SOAP) services


© 2011 Microsoft
Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademar
ks
and/or trademarks in the U.S. and/or other countries.

The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the
dat
e of this presentation. Because Microsoft must respond to changing market
conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accu
rac
y of any information provided after the date of this presentation.

MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.