ado.net data services - guy smith- · pdf filecourseware online 3 agenda introduction to...

33
1 Courseware Online ADO.NET Data Services Framework (formerly known as "Astoria") Guy Smith-Ferrier [email protected] Blog: http://www.guysmithferrier.com

Upload: truongxuyen

Post on 29-Mar-2018

245 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

1Courseware Online

ADO.NET Data Services Framework

(formerly known as "Astoria")

Guy [email protected]

Blog: http://www.guysmithferrier.com

Page 2: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

2Courseware Online

About…

Author of .NET Internationalization

– Visit http://www.dotneti18n.com to

download the complete source code

The .NET Developer Network

– http://www.dotnetdevnet.com

– Free user group for .NET developers,

architects and IT Pros based in Bristol

Page 3: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

3Courseware Online

Agenda

Introduction to ADO.NET Data Services

ADO.NET Data Services URI Protocol

How to create an ADO.NET Data Server

Customizing an ADO.NET Data Server

Using the ADO.NET Data Services Client Library

Page 4: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

4Courseware Online

Information Sources ADO.NET Data Services Website and Documents

– http://astoria.mslivelabs.com

– http://astoria.mslivelabs.com/ADO.NET%20Data%20Services%20Overview_CTP1.doc

– http://astoria.mslivelabs.com/Using%20ADO.NET%20Data%20Services_CTP1.doc

Videos

– Mike Taulty's ADO.NET Data Services Screencasts

http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2008/01/17/10130.aspx

– MIX08 (3 sessions), MIX07 (1 session)

http://sessions.visitmix.com

– Channel 9 Astoria Data Services

http://channel9.msdn.com/ShowPost.aspx?PostID=305985

Podcasts

– http://www.guysmithferrier.com/downloads/PabloCastro20071108.mp3

– http://www.dotnetrocks.com/default.aspx?showNum=289

Books and Magazines

– RESTful Web Services, Leonard Richardson and Sam Ruby, O'Reilly

– The Architecture Journal (#13)

http://msdn2.microsoft.com/en-gb/arcjournal/bb201656.aspx

Page 5: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

5Courseware Online

ADO.NET Data Services

Releases May 2007 CTP

– Released at MIX 07

– Compatible with Visual Studio 2008 Beta 1 only

September 2007 CTP

– Compatible with Visual Studio 2008 Beta 2 only

December 2007 CTP1

– Many breaking changes

Beta 1 Mid-2008

RTM Later In 2008 (same time as Entity Framework)

Page 6: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

6Courseware Online

What You Need To Get Started Visual Studio 2008

XML QFE Editor

– http://go.microsoft.com/fwlink/?LinkID=104985

ADO.NET Entity Framework Beta 3

– http://www.microsoft.com/downloads/details.aspx?FamilyId=15DB9989-1621-444D-9B18-

D1A04A21B519

ADO.NET Entity Framework Tools (Dec 07 CTP)

– http://www.microsoft.com/downloads/details.aspx?FamilyId=D8AE4404-8E05-41FC-94C8-

C73D9E238F82

ASP.NET 3.5 Extensions Preview

– Includes MVC, Dynamic Data, AJAX, Entity Framework, Data Services, Silverlight Controls

For ASP.NET

– http://www.asp.net/downloads/3.5-extensions/

Microsoft ASP.NET 3.5 Extensions Preview (Dec 2007) – ADO.NET Data Services

Silverlight Add-On

– http://www.microsoft.com/downloads/details.aspx?FamilyID=FD9C2A29-7383-4B2E-9EC9-

0C6120718D4F&displaylang=en

Page 7: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

7Courseware Online

The Data Story (Before AJAX)

Database

Browser Web Server

ASP.NET Application

Postback (Data)

HTML, CSS,

JavaScript, Data

Page 8: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

8Courseware Online

The Data Story (After AJAX)

Database

Browser

AJAX Client

Web Server

ASP.NET AJAX Application

HTML, CSS,

JavaScript

Web Service

or

ADO.NET

Data

Service

Data

Page 9: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

9Courseware Online

What Is ADO.NET Data Services ?

ADO.NET Data Services is:-

– a protocol

uses HTTP

uses standard HTTP verbs (GET, POST, PUT, DELETE)

a URI protocol

– a wizard for building a Data Service

– an extensible WCF service

Page 10: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

10Courseware Online

Viewing ADO.NET Data

Services In Internet Explorer

By default ADO.NET Data Services return data

in Atom format

– Internet Explorer interprets this as a feed

To view the data as XML in Internet Explorer:-

– Select Tools | Internet Options | Content | Feeds

– Click the Settings button

– Uncheck the "Turn on feed reading view" checkbox

– Click OK, Click OK

Page 11: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

11Courseware Online

ADO.NET Data Services

URI Protocol List of entity sets

– http://localhost/Northwind/Northwind.svc

List a table

– http://localhost/Northwind/Northwind.svc/Customers

Get a row by primary key

– http://localhost/Northwind/Northwind.svc/Customers('ALFKI')

Get a row by composite primary key

http://localhost/Northwind/Northwind.svc/Order_Details(10248,11)

Get the children of a specific row

http://localhost/Northwind/Northwind.svc/Customers('ALFKI')/Orders

Get the children of a specific row and include the parent

http://localhost/Northwind/Northwind.svc/Customers('ALFKI')?$expand=Orders

Page 12: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

12Courseware Online

ADO.NET Data Services

URI Protocol (continued) Sort by City

http://localhost/Northwind/Northwind.svc/Customers?$orderby=City

Sort by City descending

http://localhost/Northwind/Northwind.svc/Customers?$orderby=City desc

Sort by City descending and then by CompanyName

http://localhost/Northwind/Northwind.svc/Customers?$orderby=City desc,

CompanyName

Page 13: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

13Courseware Online

ADO.NET Data Services

URI Protocol (continued) Getting a fixed number of rows

http://localhost/Northwind/Northwind.svc/Customers?$top=5

Omitting a fixed number of rows

http://localhost/Northwind/Northwind.svc/Customers?$skip=5

Getting a fixed number of rows and omitting a fixed number of rows

http://localhost/Northwind/Northwind.svc/Customers?$skip=5&top=5

Using a filter expression

http://localhost/Northwind/Northwind.svc/Customers?$filter=City eq 'London'

Using a complex filter expression

http://localhost/Northwind/Northwind.svc/Customers?$filter=City eq 'London'

or City eq 'Berlin'

Page 14: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

14Courseware Online

Binary Operators For Filter

ExpressionsOperator Description Example eq Equal /Customers?$filter=City eq 'London' ne Not equal /Customers?$filter=City ne 'London' gt Greater than /Orders?$filter=OrderDate gt '1998-5-1' gteq Greater than or equal /Orders?$filter=Freight gteq 800 lt Less than /Orders?$filter=Freight lt 1 lteq Less than or equal /Orders?$filter=OrderDate lteq '1999-5-4'

Page 15: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

15Courseware Online

More Operators For Filter

Expressions Logical

– and, or, not

String

– contains, endswith, startswith, length, indexof, insert,

remove, replace, substring, tolower, toupper, trim, concat

Date

– day, hour, minute, month, second, year

Maths

– round, floor, ceiling

Type

– IsOf, Cast

Page 16: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

16Courseware Online

Payload Formats Payload formats can be specified in the URL

format parameter or the MIME header

Format MIME type Support ATOM/APP application/atom+xml CTP1 (Dec 07) and beyond JSON application/json All versions XML text/xml May 07 and Sept 07 CTPs only RDF application/rdf+xml May 07 and Sept 07 CTPs only

Page 17: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

17Courseware Online

ADO.NET Data Service vs.

Custom Web Service ADO.NET Data Services might become a standard

– Minimal implementation (lower dev/test/doc time)

– Commonality between applications

lower barrier to entry for new devs

lower maintenance across applications

The interface is predictable

– AJAX controls that have a data story need to be able to predict the interface

Custom methods augment the URI protocol instead of replacing it

An ADO.NET Data Service is a WCF service

– multiple transport methods are available (web service, .NET Remoting, COM)

Supports multiple data formats

Page 18: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

18Courseware Online

Creating An ADO.NET Data Service

Create a new Web Application Project (File | New |

Project | Web Application Project)

– Select ".NET Framework 3.5" in the version selector combo box

Add an ADO.NET Entity Data Model (in Solution

Explorer, right click the project, select Add | New Item

and select ADO.NET Entity Data Model)

– Name it Northwind.edmx

– In the wizard select Generate From Database and click Next

– In the Choose Your Data Connection page:-

Select a connection for Northwind

Page 19: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

19Courseware Online

Creating An ADO.NET Data Service

(continued) Change "Save entity connection settings in Web.Config as" to

NorthwindEntities

Click Next

– Click Finish

Add a ADO.NET Data Service (in Solution

Explorer, right click the project, select Add | New

Item and select ADO.NET Data Service)

– Name it Northwind.svc and click Add

Page 20: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

20Courseware Online

Creating An ADO.NET Data Service

(continued) In Northwind.svc.cs:-

– Add "Using Model;" to the using clauses

– Replace the TODO comment with NorthwindEntities

– Add the following line to InitializeService:-

config.SetResourceContainerAccessRule(

"*", ResourceContainerRights.All);

Run the data service

Page 21: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

21Courseware Online

Customizing An ADO.NET Data

Service[WebGet]

public IQueryable<Customers> CustomersByCountry(

string country)

{

if (string.IsNullOrEmpty(country))

throw new ArgumentNullException("country",

"You must provide a country");

return from customer in

this.CurrentDataSource.Customers

where customer.Country == country

select customer;

}

Page 22: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

22Courseware Online

Add the following line to InitializeService:-config.SetServiceOperationAccessRule(

"CustomersByCountry", ServiceOperationRights.All);

Invoke the service in the browser like this:-http://localhost:49240/Northwind.svc/

CustomersByCountry?country='Belgium'

Customizing An ADO.NET Data

Service (continued)

Page 23: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

23Courseware Online

Customizing An ADO.NET

Data Service (continued)

[QueryInterceptor("Customers")]

public IQueryable<Customers>

OnQueryCustomers(IQueryable<Customers> customersQuery)

{

// throttle every query to a maximum of 3 rows

return (from customer in customersQuery

select customer).Take(3);

}

Page 24: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

24Courseware Online

Accessing An ADO.NET Data

Service Using HttpWebRequestpublic string GetData(string uri)

{

HttpWebRequest webRequest =

(HttpWebRequest) WebRequest.Create(uri);

webRequest.Method = "GET";

webRequest.Accept = "application/atom+xml";

webRequest.ContentType = "application/atom+xml";

WebResponse webResponse = webRequest.GetResponse();

Stream responseStream = webResponse.GetResponseStream();

StreamReader responseStreamReader =

new StreamReader(responseStream);

return responseStreamReader.ReadToEnd();

}

Page 25: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

25Courseware Online

ADO.NET Data Services

Client Library

The Client Library is a .NET library of classes that encapsulate

communication with a Data Service

– C:\Program Files\Reference Assemblies\Microsoft\Framework\ASP.NET

3.5 Extensions\Microsoft.Data.WebClient.dll

Any .NET application can use the Client Library

– Windows Forms

– WPF, Silverlight

– ASP.NET

– Console Applications

The library uses HTTP to get/send data in the AtomPub format

Data is represented to the client application as .NET populated

objects

Page 26: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

26Courseware Online

Using The ADO.NET Data

Services Client Library

public class Categories

{

public int CategoryID { get; set; }

public string CategoryName { get; set; }

public string Description { get; set; }

public byte[] Picture { get; set; }

}

Page 27: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

27Courseware Online

Using The ADO.NET Data Services

Client Library (continued)static void Main(string[] args)

{

WebDataContext context =

new WebDataContext("http://localhost:1234/Northwind.svc");

WebDataQuery<Categories> categories =

context.CreateQuery<Categories>("/Categories?$orderby=CategoryName");

foreach(Categories category in categories)

{

Console.WriteLine(category.CategoryName);

}

}

Page 28: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

28Courseware Online

WebDataGen.exe

WebDataGen.exe generates C# classes that correspond to

a data model exposed by an ADO.NET Data Service

To run WebDataGen.exe:-"\Program Files\Microsoft ASP.NET 3.5 Extensions\

WebDataGen.exe"

/mode:ClientClassGeneration

/uri:http://localhost:1234/Northwind.svc

/outobjectlayer:Model.cs

The result is Model.cs containing all of the client-side

proxy classes

Page 29: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

29Courseware Online

Using The ADO.NET Data Services

Client-Side Proxy Classesstatic void Main(string[] args)

{

NorthwindEntities entities = new NorthwindEntities(

"http://localhost:1234/Northwind.svc");

foreach(Categories category in entities.Categories)

{

Console.WriteLine(category.CategoryName);

}

}

Page 30: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

30Courseware Online

LINQ To ADO.NET Data Services LINQ To ADO.NET Data Services is a LINQ provider

that translates LINQ syntax into ADO.NET Data

Services requestsNorthwindEntities entities = new NorthwindEntities(

"http://localhost:1234/Northwind.svc");

var categories =

from category in entities.Categories

orderby category.CategoryName

select category;

foreach (Categories category in categories)

{

Console.WriteLine(category.CategoryName);

}

Page 31: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

31Courseware Online

ADO.NET Data Services Architecture

Database

Client

AJAX

Silverlight

Windows Forms

WPF

XBAP

PopFly

ADO.

NET

Data

Service

LINQ (IQueryable)

Data

LINQ

To

SQL

LINQ

To

Objects

LINQ

To

XML

LINQ

To

Entities

Windows

Live Data

LINQ

To

Flickr

LINQ

To

Amazon

LINQ

To

Facebook

LINQ To

ADO.NET

Data Services Data

LINQ

To

Resources

Page 32: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

32Courseware Online

Possible Future Changes To

ADO.NET Data Services

Support for accessing scalar values

ADO.NET Data Services will support heterogeneous

sets in addition to homogenous sets

Better concurrency support (using time stamps and/or

sending back all columns)

Caching support

Page 33: ADO.NET Data Services - Guy Smith- · PDF fileCourseware Online 3 Agenda Introduction to ADO.NET Data Services ADO.NET Data Services URI Protocol How to create an ADO.NET Data Server

33Courseware Online

Summary

ADO.NET Data Services offers a standard URI protocol

– The interface is predictable

Generic AJAX controls can communicate with a data source

ADO.NET Data Services are extensible and customizable

– Extensions augment the URI protocol instead of replacing it

ADO.NET Data Services supports multiple transport

protocols and multiple data formats

– ADO.NET Data Services is suitable for many different types of

clients

Microsoft are offering an ADO.NET Data Service hosting

service