Custom Data Service providersintroduction
Data Services sits above a data Service Provider, which is responsible for interacting with the underlying Data Source on behalf of the Data Service.
Data Services ships with some internal providers, and makes it possible for your to create custom providers too.
So the obvious question are ...
Do you need a Custom Data Provider?
Creating a Custom Data Service Provider is a fair amount of work, which of course provides big dividends like, allowing PE Ople to:
- Query and manipulate your data via the data Services Client in:
- Wpf
- WinForms
- SilverLight
- etc
- Query you data directly via a browser
- Query and manipulate your data via Javascript and associated frameworks like JQuery
- Query your data via knowledge worker tools like PowerPivot.
- Etc.etc.etc.
But before to creating a custom provider, see if you fall to one of the following camps, all of which has a Much easier solution:
Entity Framework
If you plan on putting a Data Service over the Entity Framework the answer are no you don ' t need to write a custom provider .
Data Services has a implementation that talks to the Entity Framework in the box.
You simply point your DataService at your strongly typed ObjectContext, and that ' s it:
public class Northwinddataservice:
Dataservice<northwindentities>
In this example northwindentities is your strongly typed ObjectContext representing the Northwind database.
For a good example of an OData Service built using the Entity Framework check-out this post.
LINQ to SQL
If you want to create a data Service over a LINQ to SQL Data Source should take a look at this project on Code Gallery .
It includes sample code you can use the extend your strongly typed DataContext class to implement Theiupdatable interface.
This on conjunction with the Reflection Provider means your can use your LINQ to SQL DataContext just as an Entity Framew Ork ObjectContext:
public class Northwinddataservice:
Dataservice<northwinddatacontext>
Reflection Provider
If you had your own class that can represent your Data Source, and it had a number of strongly typed IQueryable Propertie s like this:
public class myDataSource
{
Public iqueryable<product> Products {get {...}}
Public iqueryable<categories> Categories {get {...}}
}
The built-in reflection provider can turn this to a read-only service and infer Resourcesets, Types and Properties Autom Atically.
You can even make this read-write by extending your class to implement iupdatable:
public class Mydatasource:iupdatable
In fact this is exactly how the LINQ to SQL example above works.
For a good example of an OData Service created using the reflection provider check out Tip 56.
So when does you actually need a custom provider?
The reflection provider is very useful for some scenarios.
But is have a number of limitations that might disqualify it for your scenario:
- It is static and i.e. the shape of the service can ' t evolve over time.
- It needs CLR classes for each resourcetype, and which you might is not.
- It needs to the find Id or {Type}id properties to use as keys.
- It blindly exposes all public properties of the CLR classes.
- It gives you to control over streaming or paging.
- You can ' t take advantage of the advanced features like Open Types, which allows resources to optionally include more Open Prop Erties.
- You has less control, for example you can ' t log requests as easily or modify metadata or rename properties
- etc. etc.
If any of these is important it ' s time to create a Custom Data Service Provider ...
Creating a Data Service Provider Series
This series of posts would grow to cover all of the major DSPs interfaces and show case lots of scenarios
Part 1–intro
Part 2–iserviceprovider and DataSources
Part 3–idataservicemetadataprovider
Part 4–minimal Running Service
Part 5–query
Part 6–query Interactions
Part 7–updates
Part 8-relationships
Part 9-untyped
Possible future installments ...
Part 10–etags
Part 11–friendly Feeds
Part 12–streaming
Part 13–advanced Paging
Custom Data Service Providers