Raw Notes-OData Web Services

Thursday, March 01, 2012 / Posted by Luke Puplett /




IQueryable<T> (can be collection.AsQueryable() but collection must be the whole set)

Custom Provider

DataServiceKeyAttribute on entity defines the property that has the unique id.

Add WCF Data Service item to project.

Gives service class deriving from DataService<T> where T is provider (IQueryable)

In InitializeService override, set config options for default security.

That’s it. /Actors will correspond to public IQueryable<Actor> Actors member on provider!

Filtering and paging etc. will just work via URL.

An OData service is a RESTful WCF service so it supports custom operations, like any other service operation, and can use WebGet and WebInvoke for GET, PUT, POST, DELETE

Adding a new resource:

// in ServiceOData : DataService<T> class
public IQueryable<Person> TopPeople()
return (from p in CurrentDataSource.people
let rating = p.reviews.Average(r => r.rating)
where rating > 9
orderby rating
select p).Take(20);

Then provide access via

config.SetServiceOperationAccessRule(“TopPeople”, ServiceOperationRights.All);

Method/operation parameters passed in by URL query string.

Filters and Interceptors

Query interceptors apply to GET operations and do business rule validation, security, further filtering. E.g. only return records for the customer.

Change interceptors apply to POST, PUT, DELETE and do security, rejection, blasphemy.

Implement both with operations on the data service class using attributes, or by entity in general.

public Expression<Func<person, bool>> FilterPeople()
return (p) => p.department == “IT”;

public void FilterPersonChange(person r, UpdateOperations ops)
if (ops == UpdateOperations.Add && (p.Name == “Dave CEO”))
throw new DataServiceException(400, “No-one updates the boss.”);

Labels: , ,


Post a Comment