C# – How to know about working with ADO.Net Data Services? (FAQ)

centity-frameworklinq-to-sqlsilverlightwcf-data-services

I've been learning by doing with ADO.Net Data Services (Astoria) for the last couple of months, and while I like the technology the learning has been a real trial. Information that you need to use them effectively is spread over MSDN documentation, articles, blog posts, support forums and of course StackOverflow. This question is a place for me to share some of my hard-won findings so that someone else can benefit. I also hope for other people to contribute their best practices and FAQs, and correct my misunderstandings!

For full disclosure, I've been using the framework with Linq to SQL just to make my life more complicated, so I hope the details in my answers are appropriate for Entity Framework too.

To start with below are some links I've found essential. I'll then put topic specific bits in the answers section.

Useful Links

Best Answer

Service Operations

Sometimes being able to query data and perform simple updates or creations isn't enough - you might want to implement some business logic or some complex query creation that's not possible through the URI scheme. Data Services supports this in a very basic form with Service Operations.

These allow you to add methods to your service, but with some restrictions:

  1. You can only use basic types or entity types (i.e. types already exposed by the service).
  2. Method parameters can only be simple types that can be expressed as part of a URL.
  3. No code is generated for Service Operations by datasvcutil, so you need to add them to the client libraries yourself.
  4. If you return a entity type but don't have anything to return, i.e. the result is null, then you'll get a 404 as the HTTP response.
  5. If you return void you won't be able to use the client data context to make the request, you'll have to use WebRequest.

Examples (ok, these are simplified so don't really need to be 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);
}