📜 ⬆️ ⬇️

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

Previous part

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

In this part you will learn:

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 obtained from the Internet. In addition, they can be read-only, they can be temporary for the current session of the application, or stored in the repository for all sessions of the application. Data in your application can be stored in various places. Your application can load data from the following places, and also save data to everything except the first one.
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 state capitals or background image.
In-memory state dictionariesTemporary storage used for short-term data storage while the application is deactivated. Temporary storage has a high speed, but the data in it is deleted after a few minutes, so that it is unreliable. Additional information will be provided in the “Restoring an application after decontamination” section.The current control that is in focus.
Isolated storageStorage representing the file system of the phone used for long-term data storage between application sessions. Isolated storage has low speed, but is reliable.User selected background color.
Remote storageStorage in 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 greatest flexibility. Remote data storage is a very extensive topic that goes beyond the scope of this article.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 download data. For example, isolated storage and remote storage are slow, so you have to contact them (or run background workers (background workers) that call 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 take a lot of time, you should save the data as early as possible and load it 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 code to store and retrieve data in a data access class. For example, if you load data in the OnNavigatedTo method overload, each page can use the same data access class. This class can load data on the first access and provide a cached copy for each subsequent access.

The FuelTracker application includes a data access class named CarDataStore , which encapsulates all the code needed to save 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 one Car object that the application should receive from the repository. The static class CarDataStore provides data access through the static Car property, as shown in the following code snippet.
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
  1. private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
  2. private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
  3. private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
  4. private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
  5. private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
  6. private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
  7. private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
  8. private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
  9. private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
  10. private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
  11. private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
  12. private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
  13. private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
  14. private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
  15. private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .
private static Car car; public static Car Car { get { if (car == null ) { // Initialize the car field using data retrieved from storage // or create a new Car object if there is no data in storage. // (The code for doing this is shown and described later.) } return car; } set { car = value ; } } * This source code was highlighted with Source Code Highlighter .

This code allows the OnNavigatedTo method, overloaded for each page, to get the value of a property, 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 method OnNavigatedTo, as shown in the following code snippet.
  1. protected override void OnNavigatedTo (NavigationEventArgs e)
  2. {
  3. base .OnNavigatedTo (e);
  4. CarHeader.DataContext = CarDataStore.Car;
  5. // ... other code ...
  6. }
* This source code was highlighted with Source Code Highlighter .

Next part

Source: https://habr.com/ru/post/118420/


All Articles