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 > Using LoadBehavior to control what happens to your...

Using LoadBehavior to control what happens to your data once it's sent to the client

Using LoadBehavior to control what happens to your data once it's sent to the client Applies to Silverlight 4 and 5 Fetching data from the server is one thing, but as we're working in a disconnected environment, we need a way to control what happens to entities when they arrive on the client, what if a version of that same Entity already exists on the client? Should it be ignored? Merged? Overwritten? In this recipe, we'll learn how to deal with these situations. Getting ready Before getting started, you have to make sure the correct SDK and assemblies to enable WCF RIA Services are available. Please refer to 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\Using_LoadBehavior_Starter\. The completed solution can be found in Chapter 11\Using_LoadBehavior_Completed\. How to do it... We're going to use the LoadBehavior parameter to decide what should happen with data once it's been sent to the client, starting from the provided starter solution. In order to do this, we need to 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 Height="30" Width="150" Margin="0,0,10,0" Content="Keep current" Command="{Binding LoadWithKeepCurrentLoadBehaviorCommand}"></Button> <Button Height="30" Width="150" Margin="0,0,10,0" Content="Merge into current" Command="{Binding LoadWithMergeIntoCurrentLoadBehaviorCommand}"></Button> <Button Height="30" Width="150" Margin="0,0,10,0" Content="Refresh current" Command="{Binding LoadWithRefreshCurrentLoadBehaviorCommand}"></Button> </StackPanel> Open HomeViewModel.cs, and add the following command definitions to the class: public ICommand LoadWithKeepCurrentLoadBehaviorCommand { get; private set; } public ICommand LoadWithMergeIntoCurrentLoadBehaviorCommand { get; private set; } public ICommand LoadWithRefreshCurrentLoadBehaviorCommand { get; private set; } Add the following using statements to HomeViewModel.cs: using System.Windows.Input; using GalaSoft.MvvmLight.Command; Add the InstantiateCommands() method to this class, and call it from the constructor: public HomeViewModel() { InstantiateCommands(); } private void InstantiateCommands() { LoadWithKeepCurrentLoadBehaviorCommand = new RelayCommand(() => LoadWithKeepCurrentLoadBehavior()); LoadWithMergeIntoCurrentLoadBehaviorCommand = new RelayCommand(() => LoadWithMergeIntoCurrentLoadBehavior()); LoadWithRefreshCurrentLoadBehaviorCommand = new RelayCommand(() => LoadWithRefreshCurrentLoadBehavior()); } Implement LoadWithKeepCurrentLoadBehavior() to execute loading of the data with the Keep Current LoadBehavior: private void LoadWithKeepCurrentLoadBehavior() { var loadOp = MovieContext.Load<Movie>( MovieContext.GetMoviesWithCategoryAndRentalHistoryQuery(), LoadBehavior.KeepCurrent, false); loadOp.Completed += (send, args) => { // handle errors etc }; } Implement LoadWithMergIntoCurrentLoadBehavior() to execute loading of the data with the Merge Into Current LoadBehavior: private void LoadWithMergeIntoCurrentLoadBehavior() { var loadOp = MovieContext.Load<Movie>( MovieContext.GetMoviesWithCategoryAndRentalHistoryQuery(), LoadBehavior.MergeIntoCurrent, false); loadOp.Completed += (send, args) => { // handle errors etc }; } Implement LoadWithRefreshCurrentLoadBehavior() to execute loading of the data with the Refresh Current LoadBehavior: private void LoadWithRefreshCurrentLoadBehavior() { var loadOp = MovieContext.Load<Movie>( MovieContext.GetMoviesWithCategoryAndRentalHistoryQuery(), LoadBehavior.RefreshCurrent, false); loadOp.Completed += (send, args) => { // handle errors etc }; } You can now build and run your solution (note: ensure you've updated the web.config file in RIAServices.WebHost with the correct connection string). To test the different load behaviors, try them before/after updating items in both the DataGrid and your database. How it works... We've started out by defining three buttons in XAML, binding their Command property to the commands we defined on our ViewModel. These will be exactued when we click the respective buttons. Each command represents a specific LoadBehavior. These LoadBehaviors can be added as a parameter to the Load<> method on the DomainContext: MovieContext.GetMoviesWithCategoryAndRentalHistoryQuery(), LoadBehavior.KeepCurrent, false); If you don't specify a LoadBehavior, the default behavior (LoadBehavior.KeepCurrent) is used. Load behaviors are used to control what happens with the entities you're loading once they reach the client, and the same entities (by their Key value) are already available on the client. If you've specified the LoadBehavior to be LoadBehavior.KeepCurrent, server-side changes to the entities that already were available on the client will not be reflected in these entities, the currently available versions on the client will keep on being used. If the LoadBehavior is set to LoadBehavior.RefreshCurrent, the entities that are fetched from the server will be used, no matter what. This ensures you'll always have the latest version of an entity, but it also implies that you'll lose client-side changes on an entity if you haven't persisted to the server before executing a new Load. If the LoadBehavior is set to LoadBehavior.MergeIntoCurrent, the entity you've just loaded from the server will be compared to the corresponding entity on the client on a property-level. Properties that have been edited on the client will keep their value, while the other properties will be set to the new values from the server. See also To learn more techniques concerning WCF RIA Services, have a 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