Create an application for Windows Phone 7 from start to finish. Part 11. Methods of storing data, using data access classes

Original author: Microsoft Developer Guidance team
  • Transfer
Previous Section The

data for your application will most likely come from an external data source. Your application will need to somehow save and load this data.

In this part you will learn:
  • What data types are supported in Windows Phone.
  • What to use for long-term data storage in Windows Phone.
  • How to use data access classes to save and load data.

Data types


Windows Phone supports various types of data. Data can be of the following types: data at the page level, data at the application level, user input data, static data, data received from the Internet. In addition, they may be read-only, may be temporary for the current application session, or stored in storage for all application sessions. The data in your application can be stored in various places. Your application can download data from the following places, as well as save data to everything except the first.
Data locationDescriptionExamples
Resource files and content filesRead-only files included in your application package. Resource files are loaded automatically when loading the DLLs that contain them. Content files are loaded when you access them.List of capitals of states or background image.
In-memory state dictionariesTemporary storage used for short-term data storage while the application is deactivated. Temporary storage is fast, but the data in it is deleted after a few minutes, so it is unreliable. Additional information will be provided in the part “Restoring the application after deactivation”.The current control in focus.
Isolated storageA repository representing the file system of a telephone used for long-term storage of data between application sessions. Sandbox storage is slow but reliable.User selected background color.
Remote storageA storage on the cloud or on the Internet used to exchange data between multiple applications or multiple instances of a single application. Remote storage has low speed, asynchronous access and is sometimes unavailable, but provides the most flexibility. Remote data storage is a very extensive topic that is beyond the scope of this article.A web service that returns a list of wines.
From the moment your application starts and runs, there are various places where you can save and from which you can load data. For example, isolated storage and remote storage are slow, so you must access them (or run background workers that access them) in the override of the OnNavigatedTo method of the first page loaded, and not in the Launching and Activated event handlers .

Note:
If the save and load operations are time consuming, you should save the data as early as possible and load them as late as possible. However, you should avoid loading data in the Launching and Activated event handlers.

Using data access classes


It is useful to encapsulate the data storage and retrieval code in a data access class. For example, if you load data in an overload of the OnNavigatedTo method, each page can use the same data access class. This class can load data on the first call and provide a cached copy for each subsequent call.

FuelTracker includes a data access class called CarDataStore , which encapsulates all the code needed to store and retrieve data. It includes the Car property and several methods for interacting with data.

image

Each page of the Fuel Tracker application displays data from a single Car objectThe application should receive from the repository. The CarDataStore static class provides access to data through the Car static property , as shown in the following code snippet.
  1. private static Car car;
  2. public static Car Car
  3. {
  4.     get
  5.     {
  6.         if (car == null)
  7.         {
  8.             // Initialize the car field using data retrieved from storage
  9.             // or create a new Car object if there is no data in storage.
  10.             // (The code for doing this is shown and described later.)
  11.         }
  12.         return car;
  13.     }
  14.     set { car = value; }
  15. }
* This source code was highlighted with Source Code Highlighter.

This code allows the OnNavigatedTo method, which is overloaded for each page, to get the property value, regardless of whether the data has already been loaded. For example, as described in the Data Mapping section, the FillupPage class sets the DataContext property of the user interface element to the value of the CarDataStore.Car property . This is represented in the overloaded OnNavigatedTo method, as shown in the following code snippet.
  1. protected override void OnNavigatedTo(NavigationEventArgs e)
  2. {
  3.     base.OnNavigatedTo(e);
  4.  
  5.     CarHeader.DataContext = CarDataStore.Car;
  6.  
  7.     // ... other code ...
  8. }
* This source code was highlighted with Source Code Highlighter.

Next part

Also popular now: