Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

11. Using WCF RIA Services > Controlling the server-side query from the client

Controlling the server-side query from the client

Controlling the server-side query from the client Applies to Silverlight 4 and 5 Controlling the data that's fetched is a very common requirement, instead of fetching all entities of a certain type, you might only want to fetch those that have been changed in the last week. Or you might want to fetch a specific employee from an employee database, instead of fetching all employees from the server. In the next recipe, Sorting and filtering data on the server, we'll look into doing this by writing extra query methods on our DomainService. In this recipe, we'll learn how to do this (for the most common requirements) straight from the client, without having to write extra code on the server. Getting ready Before getting started, you've got to make sure the correct SDK and assemblies to enable WCF RIA Services are available. Please refer to the recipe Setting up a data solution to work with WCF RIA Services for more information. We're starting from a provided starter solution, which you can find in Chapter 11\Controlling_Query_Starter\. The completed solution can be found in Chapter 11\Controlling_Query_Completed\. How to do it... We're going to write code to control what's returned from the server, on the client. To achieve this, we'll complete the following steps: Open Home.xaml, and locate the StackPanel named ContentStackPanel. In this StackPanel, add the following xaml before the closing tag: <StackPanel Orientation="Horizontal" Margin="0,10,0,0"> <Button Content="Order movies" Width="150" Height="30" Command="{Binding LoadOrderedMoviesCommand}"></Button> <Button Content="Filter movies" Width="150" Height="30" Margin="10,0,0,0" Command="{Binding LoadFilteredMoviesCommand}"></Button> </StackPanel> Open HomeViewModel.cs, and add the following command definitions to the class: public ICommand LoadOrderedMoviesCommand { get; private set; } public ICommand LoadFilteredMoviesCommand { get; private set; } Add the following using statements to HomeViewModel.cs: using System.Windows.Input; using GalaSoft.MvvmLight.Command; Instantiate the commands, and call the InstantiateCommands() method from the constructor: public HomeViewModel() { InstantiateCommands(); } private void InstantiateCommands() { LoadOrderedMoviesCommand = new RelayCommand(() => LoadOrderedMovies()); LoadFilteredMoviesCommand = new RelayCommand(() => LoadFilteredMovies()); } Implement the commands as follows: private void LoadFilteredMovies() { // clear the context for this demo MovieContext.Movies.Clear(); var loadOp = MovieContext.Load<Movie>( MovieContext.GetMoviesQuery().Where<Movie>(m => m.Director == "Francis Ford Coppola")); loadOp.Completed += (send, args) => { // handle errors }; } private void LoadOrderedMovies() { // clear the context for this demo MovieContext.Movies.Clear(); var loadOp = MovieContext.Load<Movie>( MovieContext.GetMoviesQuery().OrderBy(m => m.Title)); loadOp.Completed += (send, args) => { // handle errors }; } You can now build and run the application (note: ensure you've updated the web.config file in RIAServices.WebHost with the correct connection string). Clicking the Order button will return the list of movies, ordered by Title, and clicking the Filter button will return a filtered collection. How it works... In steps 1 through 4, we've added the necessary XAML code for our buttons, binding them to commands we defined on our ViewModel, so these commands will be executed once the button is clicked. The magic happens in step 5: MovieContext.Load<Movie>( MovieContext.GetMoviesQuery().Where<Movie>(m => m.Director == "Francis Ford Coppola")); Here, we define we want to load movies, using the GetMoviesQuery() method. This (auto-generated) method, returning an EntityQuery<Movie>, will start loading Movies through the GetMovie method on our MovieDomainService. The special part is the .Where<Movie> clause we add to the EntityQuery<Movie> object that's returned by GetMoviesQuery(). In this clause, we define a filter, we only want to load movies directed by Francis Ford Coppola. The special thing about this is that unlike one would expect, this Where clause is reflected straight down to the query that's executed on the database. We're on the client, adding a Where clause to an EntityQuery, and this Where clause is executed on the server. We're not fetching all Movies and then filtering them on the client, we're actually only fetching the movies that correspond to the filter. This can be seen by firing up SQL Profiler and looking at the query that's executed against our DB: It's WCF RIA Services that takes care of this - it combines the server-side method, GetMovies (which returns this.ObjectContext.Movies), with the EntityQuery extension (the Where clause) we've defined on the client. In the other command we use the same technique, this time sorting the movies by title. There's more... Not everything is controllable from the client in this way, only the most commonly used LINQ operators are supported. If more control is needed, you'll have to write server-side code on the DomainService (have a look at the next recipe, Sorting and filtering data on the server, to learn how to do this). All EntityQuery extensions are defined in the System.ComponentModel.DomainServices.Client namespace (so you'll have to import this if you want to use them), and these are the ones that are supported: OrderBy, ThenBy, OrderByDescending, ThenByDescending: Used to sort dataWhere: Used to filter dataSkip, Take: Used to skip over entities from the result set, or to select a specific amount of entitiesSelect: Used to apply a selector to the EntityQuery result See also Have a look at the next recipe, Sorting and filtering data on the server, to learn how to sort and filter without relying on the client. To learn more techniques concerning WCF RIA Services, have a look at the other recipes in this chapter or in Chapter 12, Advanced WCF RIA Services.

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint