In this recipe we will discuss how to use the third-party embedded database from McObject called Perst.
Download Perst.NET from http://www.mcobject.com. You have to first register to get the download option. After you download it to your local machine, unzip, and save it to the local folder for later reference with a folder name Perst.Net
.
Navigate to the saved folder and open the PerstWP7
project and build the project. We need the PerstWP7.dll
to be added to our recipe demo. Now let's build an app similar to our first MyTasks
recipe in Chapter 1. First add a reference to PerstWP7
to the project reference.
In this recipe we will learn how we can use the Perst Database to implement our MyTask
sample.
MyTasks
project we created from Chapter 1 and rename it MyTasks_Perst
App.xaml.cs
and add the initalization code for the Perst Database, as follows:public Database Database { get; internal set; } internal void OpenPerstDatabase() { using (var stor = IsolatedStorageFile. GetUserStoreForApplication()) { if (stor.FileExists(DataGenerator.StorageName)) { InitializePerstStorage(); } } } internal void ClosePerstDatabase() { if (Database != null && Database.Storage != null) Database.Storage.Close(); } // Code to execute when the application is launching (eg, from Start) // This code will not execute when the application is reactivated private void Application_Launching(object sender, LaunchingEventArgs e) { OpenPerstDatabase(); } // Code to execute when the application is activated (brought to foreground) // This code will not execute when the application is first launched private void Application_Activated(object sender, ActivatedEventArgs e) { OpenPerstDatabase(); } // Code to execute when the application is deactivated (sent to background) // This code will not execute when the application is closing private void Application_Deactivated(object sender, DeactivatedEventArgs e) { ClosePerstDatabase(); } // Code to execute when the application is closing (eg, user hit Back) // This code will not execute when the application is deactivated private void Application_Closing(object sender, ClosingEventArgs e) { ClosePerstDatabase(); } internal void InitializePerstStorage() { var storage = StorageFactory.Instance.CreateStorage(); // Creating Instance of Perst Storage storage.SetProperty("perst.file.extension.quantum", 512 * 1024); // Initial Size set 512KB storage.SetProperty("perst.extension.quantum", 256 * 1024); // Step of storage extension 256KB storage.Open("MyTaskDB.dbs", 0); // Open Storage //Create Database wrapper over Perst Storage Database = new Database(storage, false, true, new FullTextSearchHelper(storage)); Database.EnableAutoIndices = false; //Turn off auto-index creation (defined manually)
MainPage.xaml
should look exactly the same but the data list is retrieved from the local Perst database. Please refer to the Chapter 1 MyTasks
recipe for more information. Let's replace the InitializeTasks
method with the following:private void InitializeTasks() { myTasks = (from c in Database.GetTable<MyTasks>() select c).ToArray(); }
MyTasks
recipe. You can also add tasks by saving the task object.First, we initialize MyTaskDB.dbs
in the ApplicationStartup
event handler. We check if the file exists; if yes then we call the InitializePerstStorage
method.
Unlike with a relational database, we don't need to create any tables in the Perst database as each object can be stored individually. This can be achieved by inheriting the model class or MyTasks
class from the Persistent
class. We can also make the other fields full-text searchable by using attribute
. We can then save the form using the save()
method.
We can implement more features like Search and Delete options. Please refer to McObject's website to get the latest update and read the tutorial provided on the website for more information. Here is the link for documentation:
http://www.mcobject.com/index.cfm?fuseaction=download&pageid=642§ionid=139
Check the preceding recipe SQL CE as a local store, which is a Microsoft supported database option. Also, check Chapter 8 for how to use the MVVM (Model View ViewModel) pattern in your application to develop a more maintainable and scalable app.