DomainDataSource Support

The purpose of this tutorial is to show you how to use DomainDataSource in the context of a Silverlight application. The following common tasks will be examined:

  • Adding DomainDataSource control.

  • Configuring DomainDataSource control.

  • Loading data.

  • Consuming the result.

This tutorial will use the Northwind database, which can be downloaded from here.

Adding DomainDataSource Control

The DomainDataSource control can be found in the System.Web.Ria.Controls assembly and you have to declare the property namespace in order to use it:

xmlns:ria="clr-namespace:System.Windows.Controls;assembly=System.Windows.Ria.Controls" 

Next, just add a new DomainDataSource declaration to your resources:

<ria:DomainDataSource x:Key="DomainDataSource"> 
</ria:DomainDataSource> 

Configuring DomainDataSource Control

After declaring DomainDataSource control you should configure it to work with the already existing DomainService class. The only thing you have to do is to set the DomainContext property:

<ria:DomainDataSource x:Key="DomainDataSource"> 
    <ria:DomainDataSource.DomainContext> 
        <example:SampleRiaContext/> 
    </ria:DomainDataSource.DomainContext> 
</ria:DomainDataSource> 

Loading Data

After setting the DomainContext property, you should specify the QueryName - this is an existing method in your DomainService class which loads and returns data (in the example it is the GetCategories() method, which loads all categories from the Northwind database):

<ria:DomainDataSource x:Key="DomainDataSource" AutoLoad="True" QueryName="GetCategories" LoadedData="DomainDataSource_LoadedData"> 
    <ria:DomainDataSource.DomainContext> 
        <example:SampleRiaContext/> 
    </ria:DomainDataSource.DomainContext> 
</ria:DomainDataSource> 

Consuming the Result

Once the data is loaded and returned to your client application, the LoadedData event is fired. Via the LoadedDataEventArgs you have access to the following data:

  • AllEntities - gets all loaded entities.

  • Cancelled - gets a value indicating whether the asynchronous call has been canceled.

  • Entities - gets all top entities loaded.

  • Error - gets a value indicating which error occurred during an asynchronous operation.

  • TotalEntityCount - gets the total server entity count for the query.

  • UserState - gets the unique value for the asynchronous task.

Here is a sample code showing how to handle the LoadedData event:

private void DomainDataSource_LoadedData( object sender, LoadedDataEventArgs e ) 
{ 
    foreach ( Categories c in e.AllEntities ) 
    { 
        // Process the data 
    } 
} 
Private Sub DomainDataSource_LoadedData(ByVal sender As Object, ByVal e As LoadedDataEventArgs) 
    For Each c As Categories In e.AllEntities 
        ' Process the data' 
    Next 
End Sub 

See Also

In this article