What do I know about working with ADO.Net Data Services? (QUESTIONS AND ANSWERS)

I have been studying with ADO.Net Data Services (Astoria) for the past couple of months, and although I like the technology of learning, it has become a real challenge. The information necessary for their effective use extends to MSDN documentation, articles, blog posts, support forums, and, of course, StackOverflow. This question allows me to share some of my difficult results so that someone can win. I also hope that other people will contribute their best practices and frequently asked questions and correct my misunderstandings!

For full disclosure, I used the Linq to SQL framework to make life harder, so I hope that the details in my answers are suitable for the Entity Framework.

To start with below, some links that I found essential. Then I will put specific bits in the answer section.

useful links

+4
source share
3 answers

Service Operations

Sometimes it’s not enough to be able to request data and perform simple updates or creations β€” you may need to implement some business logic or some complex query creation that is not possible in the URI scheme. Data services support this in a very simple way through service operations.

They allow you to add methods to your service, but with some limitations:

  • You can use only base types or entity types (i.e. types already set by the service).
  • Method parameters can only be simple types, which can be expressed as part of the URL.
  • For service operations, no code is created for datasvcutil, so you need to add them to the client libraries yourself.
  • If you are returning an entity type but have nothing to return, i.e. the result is zero, then you will get 404 as an HTTP response.
  • If you return the void, you cannot use the client data context to execute the request, you will have to use WebRequest.

Examples (well, they are simplified, so there really is no need to perform service operations):

[WebGet] public Product GetProductByID(int productID) { return this.CurrentDataSource.Products.First(p => p.ID == productID); } [WebGet] public IEnumerable<Product> GetCancelledProducts(int productID) { return this.CurrentDataSource.Products.Where(p.Cancelled); } 
+2
source

Silverlight Client Library

LINQ query

At first, it seems that the linq syntax cannot be used from your context, because all requests are asynchronous, and IEnumerable does not explicitly have a BeginExecute method. To use Linq syntax, you need to specify your possible query:

 var query = (DataServiceQuery<Product>)myContext.Products.Where(p => p.SupplierID == 5); query.BeginExecute(this.HandleQueryResults, query); 

Note that the request has been submitted because you need to use the same instance of DataServiceQuery to call EndExecute, you cannot just use the context.

Change tracking

The client library does not automatically track field changes in generated types. To do this, you must implement INotifyPropertyChanged in your partial types.

Example:

 public partial class Product : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; partial void OnProductIDChanged() { FirePropertyChanged("ProductID"); } partial void OnProductNameChanged() { FirePropertyChanged("ProductName"); } private void FirePropertyChanged(string property) { ... } } 

In version 1.5, the Data Services tool can generate this for you, but it is currently only in CTP: Introduction to Data Binding in Silverlight 3 with 1.5 CTP2

Updated server data

By default, the Silverlight client context has MergeOption set to AppendOnly. This means that you will not see any changes in the objects, as soon as you request them for the first time, this is a form of caching and performance optimization. To see updates, you need to change MergeOption to OverwriteChanges, this will ensure that objects are updated. You can also throw away your context and recreate.

 myContext.MergeOption = MergeOption.OverwriteChanges 

Cross domain access

The types created by Silverlight for ADO.NET 1 Data Services use their own network stack to make more request verbs available, but unfortunately this means that cross-domain policies are not applied and you cannot execute cross-domain requests. To get around this, you can either proxy requests or wait for version 1.5 (CTP 2 is available), which supports cross-domain connectivity in Silverlight 3.

References:

+1
source

Work with Linq to SQL

You can use Linq for SQL as a read-only data context for data services right out of the box:

 public class MyService : DataService<MyLinqToSqlDataContext> 

To get update / write support, you will need an IUpdateable implementation for Linq to SQL. Fortunately, Andrew Conrad has one in the MSDN code gallery for you:

ADO.Net Data Services IUpdateable for Linq to Sql

Drop it as a partial class for your data context, and you are ready to write as well as read. Keep in mind that this implementation has some minor issues described in detail on the site, and that sometimes using Data Services with the Entity Framework has been designed to look a little smoother.

Check for Updates

When you save the changes, you will see that Linq to Sql generates a WHERE clause that checks the existing values ​​in the fields, which is not always what you want. This is really a Linq to Sql hint, not a data service, but this is the only place I use Linq for Sql. To stop this behavior, go to the Linq to Sql constructor and select the fields that you do not want to check. Change the UpdateCheck property to Never or OnlyWhenChanged.

+1
source

All Articles