Showing posts with label WCFRIA. Show all posts
Showing posts with label WCFRIA. Show all posts

Thursday, 2 June 2011

Demystifying WCF RIA Services Part 2

Following on from Part 1, we’ll now add insert, update and delete methods to our WCFRIA domain service.

Update your PersonService.cs class as follows:

Note I’m using attributes here to explicitly define the operations for Insert, Update and Delete.  I could have left these off as WCFRIA Services picks up Insert/Create/Add within the method for Insert, Update/Edit and Updates and Remove/Delete for deletions but I prefer to keep things explicit.

We are also overriding the Submit method here, which isn’t required but get’s called by the client proxy and passes back the changed item set before each of the the insert/update/delete methods are called for the individual changed items.

We'll keep the GUI real simple with just a datagrid, save button and status textblock but you'll see how powerful the grid is shortly: MainPage.xaml

Finally update your code behind on the server, note the callback methods for each of the service calls.  Remember all service calls are async in Silverlight and I reckon including the call back methods to response to any errors that might have occurred during the service call: MainPage.xaml.cs

On start up our service loads the data grid with data:

ScreenShot066

As our generated strongly typed Person object knows an Age is both required and an integer, the data grid automatically picks up the validation notifications and displays them to the user.

ScreenShot067

When we update the data with valid info and click save our success message shows us our service call encountered no errors.

ScreenShot068

The source code for this project can be downloaded here:

http://stevenhollidge.com/blog-source-code/WcfRiaWithPocos-part2.zip

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.

Saturday, 21 May 2011

WCF RIA Services Overview

Deborah Kurata gave an excellent session at the recent TechEd 2011 that serves as a great introduction to WCF RIA Services in Silverlight. I see this as an excellent resource that can be used to bring up to speed members of the team that have been focused on other technologies in the past.

To view or download the video direct you can visit the Channel9 website:

http://channel9.msdn.com/Events/TechEd/NorthAmerica/2011/DEV210