17
ADO.NET Data Services ゥ 2008 E4D Solutions LTD. All rights reserved. This presentation is for informational purposes only. 1 Data Services Eyal Vardi CEO E4D Solutions LTD Microsoft MVP Visual C# blog: www.eVardi.com 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected] Agenda Services as Resources URL Syntax Client Applications of Data Services CRUD API ASP.NET AJAX Client Library Advance Data Services

WCF Data services

Embed Size (px)

Citation preview

Page 1: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 1

Data Services

Eyal VardiCEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.eVardi.com

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Agenda Services as Resources

URL Syntax

Client Applications of Data Services

CRUD API

ASP.NET AJAX Client Library

Advance Data Services

Page 2: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 2

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

The Goals Expose data as a data service that can be

consumed by web clients.

The data service is reachable over regular HTTPrequests.

Standard HTTP verbs such as GET, POST, PUT andDELETE are used to perform CRUD operationsagainst the service.

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Why Data Services The use of web-friendly technologies make it

ideal as a data back-end for: AJAX-style applications

Rich Interactive Applications (Silverlight)

Other applications that need to operate againstdata that is across the web.

Page 3: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 3

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Services Platform

System.Net

WCF

WorkflowServices

DataServices

Application / Service

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Data Services Architecture

SQL Source

Custom LINQProvider

EntityFramework

Data Service Runtime

Hosting / HTTP Listener

Iqueryable IUpdatable

HTTP Request / Respond

Page 4: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 4

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Hello World

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Query string options $Expand

/Customers(ALFKI)?$expand=Orders.Employees

$Orderby /Customers?$orderby=City desc, CompanyName

$Skip /Customers?$skip=10

$Top /Orders?$orderby=TotalDue&$top=5

$Filter /Customers?$filter=City eq ‘London’

Page 5: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 5

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Expression SyntaxOperator Description Example

Logical Operatorseq Equal /Customers?filter = City eq 'London'ne Not equal /Customers?filter = City ne 'London'gt Greater than /Product?$filter = UnitPrice gt 20

gteq Greater than or equal /Orders?$filter = Freight gteq 800lt Less than /Orders?$filter = Freight lt 1

lteq Less than or equal /Product?$filter = UnitPrice lteq 20and Logical and /Product?filter=UnitPrice lteq 20 and UnitPrice gt 10or Logical or /Product?filter=UnitPrice lteq 20 or UnitPrice gt 10not Logical negation /Orders?$ ?$filter=not endswith(ShipPostalCode,'100')

Arithmetic Operatorsadd Addition /Product?filter = UnitPrice add 5 gt 10sub Subtraction /Product?filter = UnitPrice sub 5 gt 10mul Multiplication /Orders?$filter = Freight mul 800 gt 2000div Division /Orders?$filter = Freight div 10 eq 4mod Modulo /Orders?$filter = Freight mod 10 eq 0

Grouping Operators( ) Precedence grouping /Product?filter = (UnitPrice sub 5) gt 10

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Expression Syntax Cont.String Functionsbool contains(string p0, string p1)bool endswith(string p0, string p1)bool startswith(string p0, string p1)int length(string p0)int indexof(string arg)string insert(string p0, int pos, string p1)string remove(string p0, int pos)string remove(string p0, int pos, int length)string replace(string p0, string find, string replace)string substring(string p0, int pos)string substring(string p0, int pos, int length)string tolower(string p0)string toupper(string p0)string trim(string p0)string concat(string p0, string p1)

Date Functionsint day(DateTime p0)int hour(DateTime p0)int minute(DateTime p0)int month(DateTime p0)int second(DateTime p0)int year(DateTime p0)

Math Functionsdouble round(double p0)decimal round(decimal p0)double floor(double p0)decimal floor(decimal p0)double ceiling(double p0)decimal ceiling(decimal p)

Type Functionsbool IsOf(type p0)bool IsOf(expression p0, type p1)<p0> Cast(type p0)<p1> Cast(expression p0, type p1)

Page 6: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 6

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Data ServicesURL Syntax

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

LINQ to URI /ds.svc/Customers

from c in Customersselect c

/ds.svc/Customers('ALFKI') ( from c in Customers

where c.keyProperty == "ALFKI"select c ).First()

/ds.svc/Customers('ALFKI')/Address ( from c in Customers

where c.keyProperty == "ALFKI"select c.Address ).First()

/ds.svc/Customers('ALFKI')/Orders from c in Customers

from c2 in c.Orderswhere c.keyProperty == "ALFKI"select c2

Page 7: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 7

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Query Interceptors Query interceptors are useful when you want

to run your own custom methods whenqueries are executed against your entity sets.

[QueryInterceptor("Employees")]public Expression<Func<Employees,bool>> FilterEmployees(){

return e => e.EmployeeID > 0;}

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Change Interceptors Change interceptors execute code during

inserts, updates and deletes. We can define a change interceptor to perform any

validations etc during inserts, updates and deletes.

[ChangeInterceptor("Customers")]public void AssignDefaultCountry(Customer c,

UpdateOperations operation){

if (operation == UpdateOperations.Add) {if (c.Country.Trim().Length == 0) {

c.Country = "UK";}

}}

Page 8: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 8

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Extending Data Services You can extend ADO.NET Data Services by

providing your own methods called as serviceoperations to your data services.

[WebGet]public IQueryable<Customer> GetCustomersByCity(string city){

IQueryable<Customer> query =from c in this.CurrentDataSource.Customerswhere c.Country == cityselect c;

return query;}

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Data Service Access PolicyRights Description

None Denies all rights to access data.

ReadSingle Authorization to read single data items.

ReadMultiple Queries for the entire contents of the set are allowed.

AllRead Shorthand for ReadSingle | ReadMultiple

WriteAppend Authorization to create new data items in data sets.

WriteReplace Authorization to replace data.

WriteDelete Authorization to delete data items from data sets.

WriteMerge Shorthand for WriteDelete| WriteReplace| WriteAppend.

AllWrite Authorization to write data.

All Authorization to create, read, update, and delete data.

Page 9: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 9

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Example

public class Northwind : DataService<NorthwindEntities>{

public static void InitializeService(IDataServiceConfiguration config)

{config.SetEntitySetAccessRule(

"*", EntitySetRights.All );config.SetServiceOperationAccessRule(

"GetEmployee", ServiceOperationRights.All);}

}

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

IDataServiceConfiguration

Page 10: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 10

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Extending DataServices

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

In Memory Data ADO.NET Data Services is very extensible and

can work with in-memory data (such as a listof objects) as well as data from databases.

public class CustomerContainer{

private List<Customer> customers = new List<Customer>();public IQueryable<Customer> Customers{

get{ return customers.AsQueryable(); }}

}

Page 11: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 11

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Derive Data Service Class CreateDataSource()

Creates a data source of the template class that will be used by thedata service.

HandleException( HandleExceptionArgs args ) Called when an exception is thrown while processing a request.

OnStartProcessingRequest( ProcessRequestArgs args ) Called before processing each request. For batch requests, it is

called once for the top batch request and once for each operation inthe batch.

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Derive DataServiceClass

Page 12: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 12

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Custom Provider (IUpdatable)

The main design goals of IUpdatable interfaceis to make it resource independent. The methods that return objects representing resources can

return anything.

The actual implementation of IUpdatableneeds to track the mapping between theopaque object to the actual object itrepresents.

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

IUpdatable

Page 13: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 13

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

IUpdatable

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Entity Customization

ETag Attribute

IgnoreProperties Attribute

MimeType Attribute

SingleResult Attribute

Page 14: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 14

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Data Services Client Library

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Client API The goal of System.DataServices.Client is:

Object deserialization

Lazy loading

Identity tracking

Change tracking

DataServiceContext context =new DataServiceContext(

new Uri( "http://localhost:5000/Northwind.svc" ) );

Page 15: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 15

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Object Deserialization ADO.NET Data Services comes with a tool to

generate your client-side model.(DataSvcUtil.exe)

Uri ds = new Uri( "http://E4D.com/Northwind.svc" );DataServiceContext context = new DataServiceContext( ds );DataServiceQuery<Employees> employees =

context.CreateQuery<Employees>("Employees?$filter= EmployeeID gt 5" );

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Lazy Loading $extend LoadProperty

Uri ds = new Uri( "http://E4D.com/Northwind.svc" );DataServiceContext context = new DataServiceContext( ds );DataServiceQuery<Employees> employees =

context.CreateQuery<Employees>("/Employees");foreach (var item in employees){

// item.Orders.Count = 0.context.LoadProperty(item, "Orders");// item.Orders.Count = ?.

}

Page 16: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 16

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Identity Tracking DataServiceContext class has identity

tracking built into it.

If you request object, and then request thesame object again, it will be smart enough togive you the existing instance and not makethe service call. Context.MergeOption = MergeOption.AppendOnly;

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Identity TrackingMergeOption

Member Description

AppendOnly Objects that already exist in the DataServiceContext are not loaded from persistedstorage. This is the default behavior for queries or when calling the Load method onan EntityCollection<(Of <(TEntity>)>).

OverwriteChanges Objects are always loaded from persisted storage. Any property changes made toobjects in the DataServiceContext are overwritten by the storage values.

PreserveChanges When an object exists in the DataServiceContext, it is not loaded from persistedstorage. Any property changes made to objects in the DataServiceContext arepreserved. This is used to force changes to objects in the DataServiceContext tosave successfully after an OptimisticConcurrencyException has occurred. For moreinformation, see Saving Changes and Managing Concurrency in the ADO.NET EntityFramework documentation.

NoTracking Objects are maintained in a Detached state and are not tracked in theObjectStateManager.

Page 17: WCF Data services

ADO.NET Data Services

© 2008 E4D Solutions LTD. All rights reserved.This presentation is for informational purposes only. 17

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Change Tracking Make a bunch of changes in a single session

and then submit all those changes forpersistence in one swoop. DataServiceContext

.SaveChanges( SaveChangesOptions options );

© 2008 E4D Solutions LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

CRUD APIMethod Description

AddObject Adds a newly created object.

AttachTo Start tracking object.

UpdateObject Marks an object as changed.

DeleteObject Marks the object for deletion.

AddLink Adds a link between two objects.

AttachLink Start tracking between two objects.

UpdateLink Updates a link between two objects.

DeleteLink Deletes a link between two objects.