XPO – 11.1 Sneak Peek – IDataStore WCF Service in Silverlight

XPO Team Blog
16 May 2011

In this blog post we will talk about peculiarities of using IDataStore WCF services in Silverlight projects. As you guess, the main restriction is that all inquiries should be performed asynchronously or rather should not block the UI thread.

Let’s extend the solution from the WCF services for IDataStore blog by adding a new Silverlight Application project to it. Then, we will add references to DevExpress.Data and DevExpress.Xpo assemblies and an existing  Persistent.cs file with the Customer class definition.

Next, we will drop the GridConrtol (gridControl1) component onto the main page and modify the MainPage.xaml file as shown below:

  1: <UserControl x:Class="SilverlightApplication1.MainPage"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5:     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6:     mc:Ignorable="d"
  7:     d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
  8:              xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid">    
  9:     <Grid x:Name="LayoutRoot" Background="White">
 10:         <dxg:GridControl x:Name="gridControl1" AutoPopulateColumns="True">            
 11:         </dxg:GridControl>
 12:     </Grid>
 13: </UserControl>

Finally, we will modify the MainPage class code in the following way:

  1: using DevExpress.Xpo;
  2: using DevExpress.Xpo.DB;
  3: ...
  4: namespace SilverlightApplication1 {
  5:     public partial class MainPage : UserControl {
  6:         Session session = null;
  7:         public MainPage() {
  8:             InitializeComponent();
  9:             //Initialize connection settings in a separate thread.
 10:             ThreadPool.QueueUserWorkItem(o => {
 11:                 //Create connection to our WCF Service.
 12:                 XpoDefault.DataLayer = XpoDefault.GetDataLayer(
 13:                     "http://localhost:64466/Service.svc",
 14:                     AutoCreateOption.SchemaAlreadyExists
 15:                 );
 16:                 session = new Session();
 17:                 //It is necessary to call UpdataSchema method for all persistent classes.
 18:                 session.UpdateSchema(typeof(Customer));
 19:                 Dispatcher.BeginInvoke(BeginInitializeDataSource);
 20:             });
 21:         }
 22:         void BeginInitializeDataSource() {
 23:             var query = from c in session.Query<Customer>()
 24:                         where c.Country.Length > 2
 25:                         select c;
 26:             //Execute the query asynchronously.
 27:             query.EnumerateAsync(EndInitializeDataSource);
 28:         }
 29:         void EndInitializeDataSource(IEnumerable<Customer> result, Exception ex) {
 30:             //Assign the data source to the control.
 31:             gridControl1.DataSource = result;
 32:         }
 33:     }
 34: }

If we run the application, we will see the following results:


Happy XPOing in Silverlight!Winking smile

What’s New in v20.1

To explore the scope, breadth and capabilities of our new products/features, please visit: https://www.devexpress.com/Subscriptions/New-2020-1.xml.
4 comment(s)
Michael Proctor [DX-Squad]
Michael Proctor [DX-Squad]

Yay more Async support for XPO.

I still wonder how XPO handles Delayed properties with SL/WPF as that currently blocks the UI thread while the session goes and collects the item.

16 May 2011
Hideaki Kusaka
Hideaki Kusaka

session.Query<Customer>() --> new XPQuery<Customer>() ??

16 May 2011
Alex M (DevExpress)
Alex M (DevExpress)

Hideaki, yes, that's right. To be precise,

session.Query<Customer>() --> new XPQuery<Customer>(session)

This is another enhancement we have added to v2011 vol 1 - the capability to create a new XPQuery<T> instance instantly, from your session's scope. We hope you will find this enhancement useful.

17 May 2011
Murat_ Yüceer
Murat_ Yüceer

seems nice! any news for paging large datasets?

17 May 2011

Please login or register to post comments.