Sunday, November 14, 2010

Entity Framework 4 and WCF Services

Recently I have been reading about using entity framework 4 with WCF services. There are array of options for building services, each option servers a different purpose. As I have played around with various options, I want to talk about which solution(s) applies to different requirements.

POCO entities or EntityObects?
EntityObjects are the “out of the box” option, while they provide a lot of useful automated change tracking and relationship management, it is challenging to work with services that depend on EntityObjects and transfer them across tiers. POCO(Plain Old CLR Objects) entities remove lot of extra layers concerning state management, when you are creating services and making them easy for end users to consume.

Custom service, data service, or RIA service?
There are three paths to choose for WCF services. The first is to write your own service. This is where you have full control over the service operations and the other logic, including handling features such as security.

WCF Data Services is a more lightweight solution and allows you to provide your data to a wide range of consumers. Unlike WCF services, which use SOAP to move data across the wire, WCF Data Services exposes your data for access through URIs over REST (i.e., directly through HTTP). There are also convenient client APIs for .NET, Silverlight, PHP, AJAX, and other consumers. This approach might appear as putting your database on the Internet, which is not the case. What is exposed is first defined by your model and further refined by settings in your service. You do have some control over securing the data, but it is not the same control you can exercise with your custom services.

WCF RIA Services attempts to bridge the gap between custom services and WCF Data Services. WCF RIA Services was originally designed to help getting data into and out of Silverlight applications, you can also consume RIA services from other applications as well, because in the end, unlike WCF Data Services, WCF RIA Services is still a WCF service. RIA services encapsulate some of the most common desired CRUD functionality.Though this does not still give you complete control, but if you want leverage a boxed solution which is customizable, WCF RIA Services could be a good candidate.

Self-tracking entities?
Self-tracking entities are not lightweight, and to get their true benefits, the consuming application must be a .NET 3.5 or 4 app that contains self-tracking entities. Self-tracking entities is the simplest path for using entities in WCF services. Do not mistake self-tracking entities as a great solution for all of your applications. They are written specifically to be used with custom WCF services. They will not work with WCF Data Services or WCF RIA Services, nor can you use them to solve n-tier issues in other applications.

Self-tracking entities are very different from other POCO classes, whether your POCO classes are generated from the provided template, a customized version of that template, or your own template.

It is very important to understand what different options are available, so we can make the right choice when exposing business model through WCF.

Reference: Programming Entity Framework, Second Edition, by Julia Lerman.

No comments:

Post a Comment