While these features work well, there's a price in terms of complexity because they rely on an asynchronous programming model and require code to be synchronized between Silverlight and server projects. Microsoft recognized the challenges of the asynchronous model and that sharing data validation rules between projects could be tricky. To simplify the process, it has developed WCF RIA Services, a data exchange and validation framework for Visual Studio 2008 and Visual Studio 2010.
Calling WCF RIA Services
Once WCF RIA Services is downloaded from www.silverlight.net/riaservices and installed, the domain service class can be called from a Silverlight application without having to go through the process of generating a client-side proxy object using Add Service Reference. After compiling the solution, the Silverlight project adds a Generated _Code folder that contains all of the code needed to call the domain service.
To call a WCF RIA Services operation, you can use a client-side domain context class that's named after the domain service class that lives on the server side. This client-side class is automatically generated and added to the Generated_Code folder. If the server-side class is named NorthwindDomainService, then by default the client-side domain context class is named NorthwindDomainContext. The domain context class can be used directly in the Silverlight code-beside file or integrated into a ViewModel class in cases where the Model-View-ViewModel pattern is being used.
Looking through the example code that uses the domain context class to retrieve data from a WCF RIA service, you see that the Customers collection property stores an EntitySet<T> type as opposed to ObservableCollection<T> (see box, below). EntitySet<T> is provided by WCF RIA Services and is used to track changes that are made to objects. You also see that as the CustomersViewModel object's constructor is called, an instance of the domain context class is created and used to hook the Customers entity to a local Customers property.
|How To Retrieve Data From A WCF RIA Service|
Once the entity is assigned to the property, a call is made to GetCustomers that invokes the domain context object's Load<T> method. The type of query to load is passed into the method. GetCustomersQuery is automatically generated by WCF RIA Services based upon a domain service query named GetCustomers. The query executes asynchronously behind the scenes and automatically assigns the returned value to the CustomersViewModel object's Customers property. The sample application available at informationweek.com/1254/mdev/wcf demonstrates other features such as the ability to access a customer's orders and edit the data using a DataForm control.
WCF RIA Services provide a simple way to leverage .NET 3.5 data annotations to validate data as it's assigned to properties in entities. Data annotations located in the System.ComponentModel.DataAnnotations namespace--such as Required, RegularExpression, StringLength, and CustomValidation--can be added into a metadata class and used to validate data:
internal sealed class OrderMetadata
public string CustomerID;
public int OrderID;
In situations where custom validation is needed, the CustomValidation attribute can be added above appropriate metadata class fields. As data is assigned to the corresponding property, the metadata class can validate the data using a validation class. For instance, a DateValidator class can be used to perform custom date validation to make sure the year entered is less than or equal to the current year (see box, above). Here's an example of applying the DateValidator class to a metadata class field:
Once the project is compiled, the data annotations, including the custom annotation, are included in the Silverlight project. Controls such as the DataForm can detect whether data is valid for a given property and show error messages as appropriate.
Dan Wahlin is a Microsoft most valuable professional for ASP.NET and XML Web Services and founder of The Wahlin Group (thewahlingroup.com).
Read more about Windows development at ddj.com/windows