Quick Start Guide

This page provides a step-by-step sample of how to use the Phone Storage project.
The result sample projection is available here

Prepare the Project

First we need to create a new sample project, as provided by Microsoft through Visual Studio.

Steps:
  • Open Visual Studio 2010
  • Create a new Windows Phone Databound Project
  • Name it Phone.Storage.Sample
  • Add a reference to Phone.Storage.dll
  • Confirm that the project can be built and deployed

Update the ItemViewModel

The ItemViewModel class should be updated to integrate an identifier.

Steps:
  • Open the ItemViewModel.cs
  • Add a Identifier property that manipulates a Guid

private Guid _identifier;

public Guid Identifier
{
    get
    {
        return _identifier;
    }
    set
    {
        if (value != _identifier)
        {
            _identifier = value;
            NotifyPropertyChanged("Identifier");
        }
    }
}

Notes about stored elements:
  • An identifier is required for each stored class but this identifier can be read-only
  • The stored elements can be interfaces or abstract classes as well

Set up Initial Data

The storage engine should be configured when the application starts.
We integrate sample data into the system on the same tme.

Steps:
  • Edit the App.xaml.cs file
  • Add the following code in the Application_Closing() method:
IDataSource datasource = Engine.Instance.RetrieveDataSource("store");
if (datasource != null)
{
    datasource.RemoveEverything();
    datasource.Dispose();
}
  • Add the following code in the Application_Launching() method:
IDataSource datasource = Engine.Instance.RegisterDataSource(new DefaultDataSourceDefinition("store")
    .AddClass<ItemViewModel, Guid>(o => o.Identifier));

datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime one", LineTwo = "Maecenas praesent accumsan bibendum", LineThree = "Facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu" });
datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime two", LineTwo = "Dictumst eleifend facilisi faucibus", LineThree = "Suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus" });
datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime three", LineTwo = "Habitant inceptos interdum lobortis", LineThree = "Habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu suscipit torquent" });
datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime four", LineTwo = "Nascetur pharetra placerat pulvinar", LineThree = "Ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos" });
datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime five", LineTwo = "Maecenas praesent accumsan bibendum", LineThree = "Maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos interdum lobortis nascetur" });
datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime six", LineTwo = "Dictumst eleifend facilisi faucibus", LineThree = "Pharetra placerat pulvinar sagittis senectus sociosqu suscipit torquent ultrices vehicula volutpat maecenas praesent" });
datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime seven", LineTwo = "Habitant inceptos interdum lobortis", LineThree = "Accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat" });
datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime eight", LineTwo = "Nascetur pharetra placerat pulvinar", LineThree = "Pulvinar sagittis senectus sociosqu suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum" });
datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime nine", LineTwo = "Maecenas praesent accumsan bibendum", LineThree = "Facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu" });
datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime ten", LineTwo = "Dictumst eleifend facilisi faucibus", LineThree = "Suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus" });
datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime eleven", LineTwo = "Habitant inceptos interdum lobortis", LineThree = "Habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu suscipit torquent" });
datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime twelve", LineTwo = "Nascetur pharetra placerat pulvinar", LineThree = "Ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos" });
datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime thirteen", LineTwo = "Maecenas praesent accumsan bibendum", LineThree = "Maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos interdum lobortis nascetur" });
datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime fourteen", LineTwo = "Dictumst eleifend facilisi faucibus", LineThree = "Pharetra placerat pulvinar sagittis senectus sociosqu suscipit torquent ultrices vehicula volutpat maecenas praesent" });
datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime fifteen", LineTwo = "Habitant inceptos interdum lobortis", LineThree = "Accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat" });
datasource.Save(new ItemViewModel() {Identifier=Guid.NewGuid(), LineOne = "runtime sixteen", LineTwo = "Nascetur pharetra placerat pulvinar", LineThree = "Pulvinar sagittis senectus sociosqu suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum" });

Notes about initialization:
  • You should consider using an IApplicationService implementation to manage the data source(s) of your application
  • You should always have a call to the DataSource.Dispose() method before losing your context, this call will store all the indexes
  • The DefaultDataSourceDefinition provides also a AddIndex method.

Notes about IDataSource functionalities:
  • The IDataSource interface provides three basic actions on stored elements: Load, Save and Delete
  • Events are available to interact with the engine before and after all thore three actions
  • Provides also the RemoveEverything() method - particulary useful for testing

Retrieve and Display the Values

Now that some data are part of the system, we can retrieve them.

Steps:
  • Edit MainViewModel.cs file
  • Remove the body of the LoadData() method
  • ... and use this code as its new body:

IDataSource datasource = Engine.Instance.RetrieveDataSource("store");
if (datasource == null)
{
    datasource = Engine.Instance.RegisterDataSource(new DefaultDataSourceDefinition("store")
        .AddClass<ItemViewModel, Guid>(o => o.Identifier));
}

foreach (ItemViewModel item in datasource.Query<ItemViewModel>().AsValues())
{
    this.Items.Add(item);
}

this.IsDataLoaded = true;

Notes about Query:
  • The IDataSource.Query() method provides a list of the IDescriptor to allow Linq filters before retrieving data
  • A IDescriptor provides the identifier and the index values for a specific stored instance
  • The conversion between IDescriptor and stored objects is done by calling the AsValues() method

Related Topics

Last edited Mar 29, 2011 at 5:05 AM by Faz, version 10

Comments

No comments yet.