Tuesday, May 15, 2012

Local DataBase - Windows Phone

Hello


Architectural Overview


Basic idea is to use LINQ to SQL to retrieve info from the data base.
LINQ to SQL provide an object oriented approach to work with data base.
The object model consist primary DataContext class which works as a proxy of the local data base.



Data Context


The DataContext is a proxy , an object which represent the data base.
A data context contains a table objects which represent tables.
Each table object consists of entities which represent rows in the table.
Each entity is a CLR object with attributes which defines the data base table structure e.g. attribute of Name and Sex on the entity define column Name and Sex on the data base table.


LINQ to SQL Runtime


LINQ to SQL provides an object - relational mapping that enable your code to use LINQ to communicate with your data base.
LINQ to SQL translate on run time LINQ commands to SQL commands. When the data base return result LINQ translate the SQL to LINQ.


Similarities and differences

Similar to desktop application that uses SQL server rational data base , windows phone can also use local data base to select,insert,update and delete data by using LINQ to SQL.

The differences :

  • In windows phone application the data base run in the application process while in desktop application it runs on the server
  • In windows phone the data base is accessed only locally from the isolated storage by the owner process.
  • local data base can be accessed only by LINQ to SQL


Application Deployment


In a standard windows phone application the data base is created on the local storage the first time the application is loaded.



Source Sample
LocalDataBase_NK.zip


This sample illustrate how easy it is to use LINQ to SQL to do following :

  • create data base
  • add items
  • list items


In general you will need to add the following to files using LINQ :


We need to define two classes :

CSimpleDataContext - represent the data base.
Here Simple.sdf is the name of the data base.
The data base has one table - tableDataModel



CSimpleRowItem - represent row in the table.
Beside ItemID which is primery key, the table has another coulmn name ItemWasSelected

Notice the Table and Column attributes.

SimpleDataBase.xaml.cs


Data base is created in the following way



An item is added

The item is added to the data base only after SubmitChanges

Enumerating over the items in the table

Notice that good old foreach works here perfectly , really cool  !!

Run the application and click on button "Simple Data Base" to produce :



Click Add 3 time and then click View to produce :




Source Sample
LocalDataBase_NK.zip


This sample is based on the previous sample. But suppose i want to bind the data base to the view i.e. suppose i want to add item to the data base and immediately see it reflected in it's view using binding.


CNotifyingDataContext  - represent the data base.


CNotifyingRowItem - represent row in the table



The class inertis allready familier INotifyProertyChanged  (see e.g. blog ) and less known INotifyProertyChanging  which reduce memory consumption related to changes tracking.


BindedDataBase.xaml.cs


The page class - BindedDataBase inherits INotifyProertyChanged, this is due to NotifyingRowItems which the list box is binded to. Thus changes in the list box are reflected in NotifyingRowItems and vice versa.
NotifyingRowItems is filled using m_NotifyingDataContext.tableDataModel every time the page is navigated to






The constructor manages the creation of the data base and feeding it to the class which represent the data base






Navigating into the page loads the data from the data base into the observable collection and into the view as follows in two steps as follows
Step 1 : LINQ to SQL - Local Data Base -> m_NotifyingDataContext.tableDataModel
Step 2 : Binding - m_NotifyingDataContext.tableDataModel->NotifyingRowItems->ListBox




Adding an item is done first into the observable collection so it is updated immidiately via binding and then it is added into the data base




BindedDataBase.xaml


The list box is binded to NotifyingRowItems



Run the application and click on button "Binded Data Base" to produce :




Click three times on the Add button and you will see that the List is updated immidiately :


remark - the application does not handle change of check box value.

Nathan