find a location for property in a new city

Wednesday, 15 December 2010

WCF Data Services for using Entity Framework over REST web services

I spent some time looking into WCF Data Services 4.0 (AKA ADO.NET Data Services & Astoria) as a way to expose our Entity Data Model (EDM) over an easy to create set of RESTful / OData web services. I decided to document my findings.

WCF Data Services 4.0 is service that provides a way of exposing an EDM over RESTful (OData) web services using WCF. It means that if you have an EDM already built, the work is essentially done. The EDM can be exposed using WCF Data Services so that it can be queried directly by the client.

How to begin

I successfully set up some WCF Data Services to expose a Comment part of my model. This helped me. I did find difficulty due to our use of POCOs instead of raw Entity Framework's generated code but I found a post that helped which told me about a weird hack that overrides the CreateDataSource into the Data Service:
public class CommentsService : DataService<SocialEntities>
{
    protected override CommentingEntities CreateDataSource()
    {
        var context = new CommentingEntities();
        var tracestring = context.CreateQuery<Comment>("CommentingEntities.Comments").ToTraceString();
        return context; 
    }
}

Consuming the web services

A client can now access the data by requesting URLs. E.g. http://api.mysite.com/CommentsService.svc/Comments to get a list of messages or http://api.mysite.com/CommentsService.svc/Comments(413L) to get the message with ID 413 (the L is for long since I use long instead of int)

The querying gets a lot more complicated than that too. See how to use your browser to query the data model

There are many SDKs out there to begin developing against your WCF Data Services from a number of popular platforms since it is simply using the OData protocol (e.g. JavaScript, PHP, Ruby, Java, Objective-C, .NET).

Restricting use

You can restrict the number of results to return when returning a list of entities to, say 5, so the client can't overload the server/bandwidth.

You can also restrict access to select parts of your EDM to calm your security nerves too:
public static void InitializeService(DataServiceConfiguration config)
{
    config.SetEntitySetAccessRule("Comments", EntitySetRights.AllRead);
}

Evaluating when to use WCF Data Services

So it does look like a great way to use the existing EDM to instantly have a set of RESTful web services to consume. However I think I would only use this if the developers of the web service client were have knowledge of the data and are expecting to get stuck into some code when consuming our data services.

If were internally designing our own Silverlight apps then this is great. However, expecting an external user to write sometimes complex queries to get to our data using all of our business rules (e.g. IsDeleted = false && SiteID = 21) is a little much to be expected I feel.

Conclusion

So great when providing RESTful web services to a handful of specific clients who have knowledge of your data and business logic. Not so great for making a friendly API for the masses based on a complex EDM as you cannot expect them to understand your business rules as well as you do.

I am not claiming authority on this topic. I am just discussing my findings so far in the hope that you may find this useful. If you have anything you think I have missed please feel free to comment.

Follow britishdev on Twitter

3 comments:

  1. you can encapsulate and offer target data services

    ReplyDelete
  2. Ensure business congruity. For instance, an unprotected PC can get to be contaminated and obliterate information that you have to maintain your business.
    https://goo.gl/T1KELS

    ReplyDelete
  3. So, if you want to cut the maximum ice, then you need professional SEO service to cater to your web design needs. SEO companies leave no stone unturned while delivering their service.
    learn web design

    ReplyDelete