Wednesday 1 June 2011

Demystifying WCF RIA Services Part 1

WCF RIA Services rock for RAD development of Silverlight apps!  Why?  Because the framework takes your basic services, applies magic and voodoo then BAM! your client has access to data bindable objects and the services to access and update them.

I’m a big fan of RAD and IDE magic but only when I know exactly what’s going on.

With this blog post I aim to pull back the curtain on that voodoo and help people to understand what’s going on under the covers.

First up create a normal Silverlight application, note we are starting from scratch and will not be using the WCF RIA Business application template:

ScreenShot052

Then add WCF RIA Services to the application:

ScreenShot053

Now add a Person class to your WcfRiaWithPocos.Web project:

ScreenShot054

We are now ready to add a domain service to our Web project.  This is the service that will be automagically hosted in a WCF service by the WCF RIA Services framework.

ScreenShot055

ScreenShot056

Press CTRL + SHIFT + B to build your project and voodoo will take place….

What’s happening is that WCFRIA Services takes a look at your Web project, notices the Domain Service exposes the Person object and will generate code on your client project.  To see the file in your solution explorer you’ll need to select the Silverlight client project (WcfRiaWithPocos) and select from the Visual Studio main menu in  Project > Show All Files.  You can then see the file here under the Generated_Code folder:

voodoo

Let’s take a look at what’s within this file – bear with me it’s a lot of code but I’ll explain each section shortly:

 

public sealed partial class WebContext : WebContextBase

The first section defines a WebContext class.  This object is used for authentication and to store the user and role info (current user context).

public sealed partial class Person : ComplexObject (upgradable to Entity)

The next section is our Person object.  You can see WCFRIA has created a new Person object that specialises from the abstract class ComplexObject.  This special class lives in the System.ServiceModel.DomainServices.Client namespace and implements the following interfaces:

public abstract class ComplexObject: INotifyDataErrorInfo, IEditableObject, INotifyPropertyChanged
Note that a ComplexObject differs from Entity abstract class used when Entity Frameworks are introduced (rather than POCOs in this example) in that your original class does not require a Key attribute.  This leads to some restrictions compared to the Entity in that Complex objects cannot be shared or referenced from multiple parent instances and they do not support inheritance.
public abstract class Entity : INotifyDataErrorInfo, IEditableObject, INotifyPropertyChanged, IRevertibleChangeTracking, IChangeTracking
To update your Person class to an Entity simply add an Id property to your Person class and give it a key attribute.  As we've noticed validation kicking in on our client side we'll also add a required attribute to the Age property.

Don't forget to update the service to include a key for each object:


You can see that the code generated on your client side is now deriving from an Entity and is still raising all the events required to enable data binding to occur including validation.


public sealed partial class PersonContext : DomainContext


The final section in the generated code is for the service.  This section creates a PersonContext class (swapping the word Service for Context), default access to the hosting, an interface IPersonServiceContract from our original DomainService, a container to access collections of “Person” and a method to query our exposed service.


PersonContext class


The DomainContext is the proxy to your services and is fully explained here:


http://msdn.microsoft.com/en-us/library/ee707370(v=vs.91).aspx


Default access to the hosted service:


Container for collection access


Access to our service endpoint:


IPersonServiceContract interface, note the async nature of this interface – all service calls in Silverlight are asynchronous:


Right now we understand what's been generated we're finally ready to make use of our service on the client


Add a listbox to the MainPage.xaml file for the Silverlight client:


Finally update the code behind to use the async method call on service proxy.


wcfria-final


In the second part of this blog article I’ll show how to improve the service to incorporate Add, Edit and Delete functionality.

2 comments:

  1. Very cool. Was looking for something easy to get the very basics of RIA up and running.

    ReplyDelete
  2. These kind of posts are always inspiring and I prefer to read quality content so I am happy to find out many good point here.

    web hosting in india

    ReplyDelete