WCF Data Services, REST & OData Christoph Pletz Senior Consultant Trivadis AG Agenda REST and OData overview OData features Implementing OData clients Implementing OData services.

Download Report

Transcript WCF Data Services, REST & OData Christoph Pletz Senior Consultant Trivadis AG Agenda REST and OData overview OData features Implementing OData clients Implementing OData services.

WCF Data Services,
REST & OData
Christoph Pletz
Senior Consultant
Trivadis AG
Agenda
REST and OData overview
OData features
Implementing OData clients
Implementing OData services
Why data services?
Break data silos
Data sharing and integration
“ODBC for the web”
Big picture
producers
protocol
consumers
History
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
OData
Open Data Protocol
Design goals ...
Invent as little as possible
Very low barrier of entry
OData is a RESTful protocol
REST
Resources are the center piece
resources have addresses (URIs)
no custom behavior
Uniform interface ...
fixed set of operations (GET, POST, PUT, DELETE)
REST versus SOAP
SOAP
Operations, messages
WS-* protocols
Routing, policy, reliable messaging, message
security, asynchrony, message correlation,
distributed transactions
OData
www.odata.org
Builds on HTTP and AtomPub
Defines ...
XML + JSON data representation
Addressing scheme
Query operators
Metadata
OData servers
IBM WebSphere eXtreme
Scale REST data service
More at ...
http://www.odata.org/producers
Windows Azure
Marketplace DataMarket
OData clients
Browsers
OData Explorer
LINQPad
More at ...
http://www.odata.org/consumers
Agenda
REST and OData overview
OData features
Implementing OData clients
Implementing OData services
OData navigation
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 queries
... /Titles?$filter=AverageRating gt 4
filters
... /Titles?$orderby=AverageRating desc, ReleaseYear asc
sorting
.../Titles?$select=ShortName, ReleaseYear
projection
Paging support: $top, $skip, $skiptoken, $inlinecount
Metadata
http://odata.netflix.com/Catalog/$metadata
EF EDMX model
Operators and functions
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
LINQPad
OData Explorer
Agenda
REST and OData overview
OData features
Implementing OData clients
Implementing OData services
Generated proxy
“Add Service Reference” in VS for .NET and SL
VisualStudio OData Visualizer Extension
install using VS Extension Manager
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) {
...
Agenda
REST and OData overview
OData features
Implementing OData clients
Implementing OData services
Service implementation
HTTP
traffic
Custom providers:
- IDataServiceMetadataProvider
- IDataServiceQueryProvider
Data Services Runtime
Entity Framework
Provider
Reflection
Provider
Entity
Framework
.NET Classes
[+ LINQ provider]
Database
Data
Source
Custom
Provider
Data
Source
Updates:
- IDataServiceUpdateProvider
Special features:
- IDataServiceStreamProvider
- IDataServicePagingProvider
WCF Data Services with EF
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
public class ProductsDataService : DataService<NorthwindEntities>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("Categories", EntitySetRights.All);
config.SetEntitySetAccessRule("Products", EntitySetRights.All);
QueryInterceptor
Custom logic/security with QueryInterceptor
[QueryInterceptor("Categories")]
public Expression<Func<Category, bool>> InterceptCategoryQuery()
{
var auth = HttpContext.Current.User.Identity.IsAuthenticated;
return c => auth || c.IsPublic;
}
Atom feed customization
Customize the returned AtomPub document by
adding attributes to the conceptual EF model
<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" />
...
Atom feed customization
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
Custom service operations
Mark with WebGet, WebInvoke attributes
[WebGet]
public IQueryable<Product> GetProductByCategory(string name)
{
var q = from p in CurrentDataSource.Products
where p.Category.CategoryName == name
select p;
return q;
}
Updates (server)
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
Stream provider
Implement a class that supports the
IDataServiceStreamProvider interface
The data-service must support IServiceProvider
Reflection provider
Implement a class that has IQueriable properties
[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>();
...
}
Resources
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”
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 trademarks 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 date 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 accuracy 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.