Silverlight 4 + RIA Services – Ready for Business: Consuming Data in the Silverlight Client


To continue our series, let’s see where the fun comes in my look at how easy that is to consume from the client.  First just to help you understand what is happening behind the covers, let’s look at a code-behind solution.  In View\Home.xaml put a simple DataGrid on the form.

 <sdk:DataGrid Name="dataGrid1" Height="152" Width="692" />

Then add these lines of code to Home.xaml.cs

 

  1: var context = new DishViewDomainContext();
  2: this.dataGrid1.ItemsSource = context.Restaurants;
  3: 
  4: context.Load(context.GetRestaurantsQuery());
  5: 

 

In line 1, we create a DishViewDomainContext… notice this is the automatically generated (via an MSBuild task) from the DishViewDomainService on the server. 

In line 2, Notice we have a Restaurants property – we provide this because there is at least one query method that returns restaurants.  Notice the cool binding magic, we have not yet populated this data from the server, but we go ahead and bind it.  This avoids any kind of complex callback logic. 

In line 4, we explicitly load the data.. this is the network call, so we want to make sure it is clear to the developer when this happening.   As an argument we pass exactly which query method (and any arguments) to use.

Now we run it and we get our data..

image_thumb[20]

Very painless, but it gets even easier 😉

Go and in delete this code and the Xaml..  Then notice the DataSources window

image_thumb[80]

image_thumb[22]

Notice this is simply a visual representation of exactly what we were doing in code.

image_thumb[23]

Notice i can change what default UI to generate and which query method to use.

 

Dragging Restaurants to the form and bingo, we have our data.

image_thumb[26]

 

Run it and we have our data… what could be easier?

image_thumb[28]

Click on the column headers – notice sorting works!  There is no code you had to write on the client or server to enable it.  This just comes for free out of returning an IQuerable. 

Now let’s add paging..

First we drag and drop the DataPager control to the form, then do a bit of layout and we end up with something that looks nice

image_thumb[29]

But we still need to wireup the DataPager to the same underlying DataSource.  This is easy enough done by draging and dropping the same Restaurant element from the DataSources window to the DataPager.  You will now you did this right if the DataPager becomes enabled. 

image_thumb[32]

 

image_thumb[34]

 

Notice that paging works great!  No code needed on the client or the server, again, all done through using that simple Linq query we wrote on the server. But what about paging AND sorting… do they work well together?  Will it just sort the page of data that is loaded locally?  The answer of course is that sorting and paging compose nicely together and the sort is actually sent all the way to the data tier and executed there in a very efficient way.

 

Now let’s add filtering.  With just a bit more work in the UI, we can add a way to filter by postal code.

<sdk:Label Name="label1" Content="Postal Code:" FontSize="14" Margin="0,0,462,13" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="110,12,0,0" Name="postalCodeTextbox" VerticalAlignment="Top" Width="120" />

image_thumb[36]

Then we need to wire this up to the query.  Of course we want the filter applied as close to the data as possible.  We don’t want to download all the entities locally, then filter, nor do we want to pull them all to the middle-tier and then filter.  What we want is for the filter to be applied at the database level.  Luckily this is very easy to do with LINQ composition. 

First we select the DomainDataSource in the Document Outline view.

image_thumb[37]

 

image_thumb[38]

image_thumb[40]

Operator – typically you should set this to “Contains”.  If you use the default of “IsEqualTo” the first load (when there is no filter) will result in no results returned. 
PropertyPath – this is the property on the entity that you are filtering on… Just type in the simple name. 
Value – this is where to get the value to compare from.  It is easiest to do UI-to-UI binding to the TextBox’s Text property.

image_thumb[42]

Here is the dialog once we are done:

image_thumb[44]

And the resulting Xaml:

        <riaControls:DomainDataSource Name="restaurantDomainDataSource" AutoLoad="True" 
                                      d:DesignData="{d:DesignInstance my:Restaurant, 
                                                     CreateList=true}" 
                                      Height="0"  Width="0" 
                                      LoadedData="restaurantDomainDataSource_LoadedData_1" 
                                      QueryName="GetRestaurantsQuery">
            <riaControls:DomainDataSource.FilterDescriptors>
                <riaControls:FilterDescriptor Operator="Contains" 
                                              PropertyPath="PostalCode" 
                                              Value="{Binding ElementName=postalCodeTextbox, Path=Text}" />
            </riaControls:DomainDataSource.FilterDescriptors>
            <riaControls:DomainDataSource.DomainContext>
                <my:DishViewDomainContext />
            </riaControls:DomainDataSource.DomainContext>
        </riaControls:DomainDataSource>

image_thumb[47] 

Notice no code changes at all, and no changes to the business logic in the DomainService in particular. 

Comments (24)

  1. Paul Hester says:

    Love this stuff. However, the "Data Sources" window is not recognizing DomainContexts when using a WCF RIA Services Client library along with a simple Silverlight Application.

  2. Paul Hester says:

    Meant "..when using a WCF RIA Services Class Library". Did test my RC install using a Silverlight Business Application, and the Data Sources window is correctly populated.

  3. Xiaoying Guo says:

    Hi Paul,

    May I know how you are using the WCF RIA Services Class Library with your simple Silverlight Application? In which project that the Data Source Window doesn’t work to you? Could you please also check is the code generation (i.e. the .g.cs/vb file in the hidden Generated_Code folder) works correctly in the project that you expect to see the Data Source Window works?

    Thanks,

    Xiaoying Guo

    Program Manager, Visual Studio Business Applications Tooling

  4. Steve says:

    Is it possible to utilizing a datasource control in a MVVM setup ?

  5. Phil Harris says:

    Brad – Nice series

    We really need to see prototypes for LoB using MVVM. Putting domainDS in xaml is really not good practice, very difficult to debug and validate data flow, and difficult to have fine grained control

    Additionally, we have had a very difficult time with DataForm control, controlling visibility of buttons, detecting change / no change / comboboxes. We are using MVVM, with a M wrapping RIA on client side.

    Our app is a LoB with ~700 tables, and > 2000 stored procs. More complex, real world apps would be appreciated.

  6. TimothyP says:

    I have the same problem as Paul,

    no matter what I do the Data Sources window is never populated.

    Also, I find myself restarting vs2010 many times before

    the add new domain service wizard is populated with the entities of my entity framework model….

  7. Jian Fu says:

    I am facing the same issue as Paul & Timothy, following these RIA Silverlight tutorials in VS2010RC seems quite frustrating. Have you solved it?

    Also this makes me wonder should I keep following this series or not. WCF ria services is considered the hottest tech, if its this difficult to get the tutorial working, I bet it would be much more problematic when using it for own projects…

    Many people left many good words on the older SL3 RIA series, Should I follow that first?

  8. Hombianer says:

    I have the same problem as Paul, Timothy and others:

    no matter what I do the Data Sources window is never populated.

    Here are my steps:

    0. Start VS 2010 Ultimate RC (10.0.30128.1 RC1Rel, .NET 4.0.30128 RC1Rel, Hotfix: KB976272, Silverlight 4 Tools 10.0.30303, Silverlight 4 Toolkit November 2009)

    1. Download and open the ‘Ready for Business’-Sample from Brad

    2. build and run it => ok

    3. open ‘Data Sources’ panel (SHIFT-ALT-D)

    You find this under the menu ‘Data/Show Data Sources’

    Attention: it’s only visible, when the SL-Project is selected, there is no menu entry when the Web-Project is selected).

    => I cannot see any associated data sources in the ‘Data Sources’ panel!!

    I installed VS 2010 Ultimate RC on a XP and Windows 7 PC (32bit). Same situation!

    I had the same problem in the Preview and Beta2 Releases.

    I also wrote in the blogs to find a solution:

    1. http://forums.silverlight.net/forums/p/164300/371111.aspx#371111

    2. http://www.silverlight.net/learn/videos/all/ria-services-support-visual-studio-2010

    Because there was no solution, I was waiting now for the new release, but it’s still not working.

    What I also checked:

    – The DomainContext file was created with entities (Plate, RegistrationData, Restaurant, User)

    – Are the services available:

    http://localhost:21516/Services/BusinessApplication1-Web-UserRegistrationService.svc

    http://localhost:21516/Services/BusinessApplication1-Web-AuthenticationService.svc

    http://localhost:21516/Services/BusinessApplication1-Web-DishViewDomainService.svc

    Try to add the Data Sources (with the wizard):

    a) Service:

    http://localhost:21516/Services/BusinessApplication1-Web-AuthenticationService.svc

    => no services visible in the solution

    b) Object:

    1. select following

    BusinessApplication1.Web.RegistrationData

    BusinessApplication1.Web.User

    (the other entities are not visible: Plate, Restaurant)

    => these are now visible in the panel

    2. click on User in the panel:

    => Error:

    Could not load file or assembly ‘System.Runtime.Serialization, Version=2.0.5.0, …

    or one of its dependencies.

    3. In the project reference I can find this System.Runtime.Serialization with the required version.

    => may be this info helps to find the problem?

    Please help me!!

  9. Greg says:

    This is insane. I also cannot get the datasources panel to populate and there is NO answer anywhere on the web. How can we follow these tutorials if we can’t even get through the first example!!!!!

  10. Xiaoying Guo says:

    Hi Greg / Hombianer / Jian / Timothy,

    Paul’s problem has been resolved, which only happened in Class Library, which I guess was different from yours. Could you please also try the following?

    1. Create a Silverlight 3 projects, and use the similar way to add some objects into the DataSource Window or web services with List of data returned into the client project, and see if the Data Source Window works.

    2. Create a Silverlight Business Application, and open the MainPage.xaml, and see if the Data Source Window populate some pre-created types.

    You may need to build the project before you see the stuff in the Data Source Window.

    Please let me know as much as possible what your experience looks like in the above scenarios. It will help us to detect if it’s because something wrong with the VS installation or a bug in the RIA Services Data Binding.

    Thanks!

    Xiaoying

  11. Hombianer says:

    Hi Xiaoying

    How can I create a SL 3 project in VS2010? And what you mean with ‘use the similar way to add some objects’?

    As I wrote I build the application and run it AND than I opend the DataSource window.

    I had also no success add the DomainService manually.

    In the meantime I migrate the IssueTracker-Demo-Apps from David Poll to VS2010 RC:

    http://www.davidpoll.com/2009/11/19/silverlight-4-and-building-business-applications-pdc09-cl19/

    In this project the DataSource window list all entries and I can drag&drop them into xaml-pages!

    Summary:

    – Create own SL BizApp (create EDMX, create DomainService, generate DomainService, …)

    => empty DataSource window

    – Open Brad’s App:

    => empty DataSource window

    – Migrated IssueTracker from David

    => DataSource window has entites!

    Hombianer

  12. Xiaoying Guo says:

    Hi Hombianer,

    Silverlight 3 is integrated with VS2010, so as long as you install the VS2010, you should be able to create a Silverlight 3 project through the project template of Silverlight Application, and then selecting Silverlight 3 as the Silverlight Version in the follow-up wizard. Then could you please try to add some object data sources into the Data Source Window (you can add some system types by unchecking the "Hide System Assemblies" in the wizard) and see if it works?

    Besides, if you create a new Silverlight Business Application (instead of Silverlight Application and check WCF RIA Services) and build the project (you don’t need to add any other thing here since the Silverlight Business Application template has already contains some entities), do you see the Data Source Window populate anything?

    By the way, it may be not related, do you install VS2008 side by side with VS2010? When installing VS2010, did you use the default settings or customize the configurations?

    Thanks,

    Xiaoying

  13. Hombianer says:

    Hi Xiaoying

    1) I created SL 3 Application as you proposed, added System.Windows.Application into DataSources window. Drag&Drop item from the DataSources window into SL Designer – this works!

    2) Created SL4 Business App, build project => no items in Data Source Window.

    Add manually objects: BusinessApplication1.Web User and RegistrationData. Drag&Drop item from the DataSources window into SL Designer.

    What is strange: In Brad’s sample application, the other objects Restaurant and Plate are not selectable in the DataSource window wizard!

    3) Yes, I have installed VS2008 and 2010 side by side.

    Hombianer

  14. Jian Fu says:

    Hi Xiaoying,

    Thanks for your supports.

    1) New SL3 app, create new User object data source though the Data Sources panel.  WORKING, can drag&drop User from Data Sources onto the xaml design panel

    2) New Business app, build, double click Mainpage.xaml, choose Data Sources. NOT WORKING, nothing in Data Sources panel.

    Jian

  15. Xiaoying Guo says:

    Thank you for more details Hombianer and Jian! We are setting up an enviornment with VS2008 and VS2010 side by side and see if we can repro the issue. Jian, I’d also appreciate if you could let me know more about your enviornment when you see the issue (OS, any other version of VS is installed besides VS2010, etc.).

    Hombianer, what do you mean by "In Brad’s sample application, the other objects Restaurant and Plate are not selectable in the DataSource window wizard!"?

    Thanks,

    Xiaoying

  16. Hombianer says:

    Hi Xiaoying

    Im mean the ‘Ready for Business’-Sample from Brad from this blog.

    Brad defined there two Domain Entities: Plate and Restaurant (see Data Sources – screeshot from this blog).

    I wanted to create this object in the Data Source Window with the wizard (choose DataSource Type: object, select the objects from the reference assembly).

    Hombianer

  17. Xiaoying Guo says:

    Hi all,

    Thanks to the help from Hombianer, we found a bug in RC release that Data Binding and Live IntelliSense feature assemblies are not installed correctly if the VS2010 is not instealled into the default system root. If this is your case, and you see the Data Source Windows doesn’t populate the DomainContext in a RIA Services project, please try to copy the assemblies under $(ProgramFiles)Microsoft Visual Studio 10.0Common7IDEExtensionsMicrosoftRiaTools to your VS installation folder.

    If this isn’t your case but you see the problem, or after you try this it still doesn’t work, please let me know.

    Thanks!

    Xiaoying

  18. Jian Fu says:

    Hi Xiaoying,

    Thanks for your support..

    Today, I formatted c drive and reinstalled OS, and VS2010RC (full installation, installed to D DIRVE). Problem is still there…Nothing in data source panel…

    Just read a post saying that intalling VS to the default directory will solve the problem, can you comfirm on that?

    BTW, I think its better to redirect us (not only the ones here) reporting to a single problem solving site

    (such as https://connect.microsoft.com/VisualStudio/feedback/details/520414/wcf-ria-services-datasource-panel-not-populating?wa=wsignin1.0, this one is closed already)

    rather than discuss this issue in the tutorial posts.

    The tutorial itself is a wonderful learning source, really great work. Keeping posting info here will pollute it and distract other readers..

    Jian

  19. Xiaoying Guo says:

    Hi Jian,

    We’ve created a bug for this and will fix that in RIA Services RTW. For now, please try the following workaround: copy the assemblies under your system root $(ProgramFiles)Microsoft Visual Studio 10.0Common7IDEExtensionsMicrosoftRiaTools to your VS installation folder.

    For example, if your system root is C:, while you install VS in D:Program FilesMicrosoft Visual Studio 10.0, then copy the dlls under C:Program FilesMicrosoft Visual Studio 10.0Common7IDEExtensionsMicrosoftRiaTools to D:Program FilesMicrosoft Visual Studio 10.0Common7IDEExtensionsMicrosoftRiaTools. You may need to create some folders in your D: disk if they are missing.

    If it still doesn’t work, please let me know.

    Thanks,

    Xiaoying

  20. Jian Fu says:

    WORKING PERFECTLY… GREAT JOB… 🙂

  21. MawashiKid says:

    Hi Brad,

    I just migrated to VS 2010 Ultimate RTM + Silverlight 4 RC2 recently, so I thought it might not hurt spending some time reading your articles to freshen things up. Great job…Did some tests with RIA + Business App and EVERYTHING IS WORKING PERFECTLY 🙂

    Just to mention…I couldn’t get access to main images from thumbnail images… but don’t worry that’s just a small detail… Thank’s…

  22. Bill says:

    I had the same problem with the Data View not populating, extremely frustrating and rather hard to find an answer to.

    In any case I too installed VS 2010 in an alternate location.

    I compiled the RiaTools folder to th ecorrect location and everything works perfectly!

    Thanks Xiaoying!

  23. Baba says:

    Almost every blog has the same type of Grid binding example…………..Is Grid the only control used in business applications ?  I think real world is lot more complex than displaying data in the grid, so please dare do something different.

  24. anotherdev says:

    Brad, great job and great tutorials. Thank you.

    Baba, we are here to learn SL4+RIA Services basics, not to learn how to customize a datagrid.

    You’ll find a lot of examples for that, using Blend or "handwritten XAML", all over the web.

    BTW, the datagrid is the default control for viewing lists but VS2010 allows us to change this default control and even use a custom one.