Windows 8 programming is very powerful, but Microsoft does not have a good story for local storage. The best they have to offer for local relational databases is to recommend using SQLite. Telerik has stepped into this breach to offer a Local Data Storage solution.

image

Installation

For now, to use Telerik Data Storage you must install it separately. To do so go to your Telerik account and click on Products & Subscriptions and then click on the DevCraft Ultimate link or the Windows 8 product link. On the next page click the Download Installer and other resources button. Find RadControls for Windows 8 and click on Browse all product files. Scroll to the bottom and click on XAML Data Storage. This will download a zip file that contains the Data Storage SDK, an example application and short documentation. Extract Telerik.Storage.Xaml.vsix to install the SDK.

Creating A Sample Application

To see Local Data Storage at work, let’s create a Windows 8 Telerik application. Once you have the application open, add two references: one to Telerik Data Storage for Windows 8 XAML and one to Microsoft Visual C++ Runtime Package.

The next step is to create data. We’ll be creating an application that displays cars and their owners and lets us add and delete cars to the collection. To get started create the Car class,

public class Car
{
[Key]
[DatabaseGenerated( DatabaseGeneratedOption.Identity )]
public long CarID { get; set; }
public string Model { get; set; }
public DateTime YearOfModel { get; set; }
public string RegistrationNumber { get; set; }
public long OwnerFK { get; set; }
}

Note the attributes added to the CarID. The first, Key indicates that this is a key (unique, indexed) field. The second, DatabaseGenerated, with the Identity option, indicates that a unique value will be assigned by the databse.

The final property in this class is OwnerFK. FK stands for Foreign Key, and indicates that there will be another class with an Owner ID that this relates to. Let’s create that class now,

public class CarOwner
{
[Key]
[DatabaseGenerated( DatabaseGeneratedOption.Identity )]
public long OwnerID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public bool Alive { get; set; }
public DateTime DateBorn { get; set; }
}

As we expected, the class has an OwnerID which will act as a Foreign Key in the Car class. This creates a one to many relationship where each owner may have many cars, but every car has one owner.

Our User Interface is very simple. We have a row of buttons below which we have a RadDataGrid. To create the two rows, use the following RowDefinitions,

<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>

To create the buttons, add a StackPanel with Horizontal orientation,

<StackPanel Orientation="Horizontal">
<Button Name="Create"
Content="Create"
Click="Create_Click" />
<Button Name="Add"
Content="Add"
Click="Add_Click" />
<Button Name="Delete"
Content="Delete"
Click="Delete_Click_1" />
<Button Name="ViewCars"
Content="ViewCars"
Click="ViewCars_Click_1" />
<Button Name="ViewOwners"
Content="View Owners"
Click="ViewOwners_Click_1" />
</StackPanel>

Finally, create the RadDataGrid,

<Grid:RadDataGrid Name="dataGrid" Grid.Row="1"/>

The Grid namespace is created for you when you drag the RadDataGrid onto the code from the toolbox.

xmlns:Grid="using:Telerik.UI.Xaml.Controls.Grid"

The Context

In MainPage.xaml.cs we’ll create an object of type Context to represent the local data storage,

Telerik.Storage.Extensions.Context context;

We can initialize this object in the constructor,

public MainPage()
{
this.InitializeComponent();
context = new Context( "CarsDBDemo" );
}

When we navigate to the page we’ll display the cars by querying the context and setting the ItemsSource property of the DataGrid,

protected override void OnNavigatedTo(NavigationEventArgs e)
{
var cars = from item in context.GetAll<Car>()
select item;
dataGrid.ItemsSource = cars;

}

Handling the Events

All that’s left is to handle the button events. The key event is Create which creates the two tables and populates them with sample data.

private void Create_Click( object sender, RoutedEventArgs e )
{
context.Insert<CarOwner>( new CarOwner()
{ Name = "Paul Grohe",
Age = 33,
Alive = true,
DateBorn = new DateTime( 1980, 7, 23 ), } );
context.Insert<CarOwner>( new CarOwner()
{ Name = "Alex Claude",
Age = 33,
Alive = true,
DateBorn = new DateTime( 1980, 7, 23 ), } );
context.Insert<CarOwner>( new CarOwner() {
Name = "Stephen Dred",
Age = 33,
Alive = true,
DateBorn = new DateTime( 1980, 7, 23 ), } );
context.Insert<CarOwner>( new CarOwner() {
Name = "Grouch Marx",
Age = 33,
Alive = true,
DateBorn = new DateTime( 1980, 7, 23 ), } );
context.Insert<CarOwner>( new CarOwner() {
Name = "Karl Marx",
Age = 33,
Alive = true,
DateBorn = new DateTime( 1980, 7, 23 ), } );
context.Insert<Car>( new Car()
{ Model = "BMW",
RegistrationNumber = "ZD123DF",
YearOfModel = new DateTime( 2010, 1, 1 ),
OwnerFK = 1 } );
context.Insert<Car>( new Car()
{ Model = "Lancia",
RegistrationNumber = "WD4313FF",
YearOfModel = new DateTime( 2008, 1, 1 ),
OwnerFK = 2 } );
context.Insert<Car>( new Car()
{ Model = "Audi",
RegistrationNumber = "KD2233DD",
YearOfModel = new DateTime( 2011, 1, 1 ),
OwnerFK = 3 } );
context.Insert<Car>( new Car()
{ Model = "Mercedes",
RegistrationNumber = "PF123ML",
YearOfModel = new DateTime( 2012, 1, 1 ),
OwnerFK = 4 } );
context.Insert<Car>( new Car()
{ Model = "Toyota",
RegistrationNumber = "JD149KL",
YearOfModel = new DateTime( 2009, 1, 1 ),
OwnerFK = 5 } );
context.SaveChanges();

DisplayCars();
}

Let’s break this down. We begin by calling Insert on the Context object, indicating the type of object we’re going to insert (CarOwner) and then passing in a new CarOwner object initialed appropriately.

We do this for five CarOwner objects and then we insert five Car objects, again passing in new Car instances properly initialized.

We then call SaveChanges on the context to update the database.

Finally we call DisplayCars to select all the cars from the database and display that in the DataGrid,

private void DisplayCars()
{
var cars = from item in context.GetAll<Car>()
select item;
dataGrid.ItemsSource = cars;
}

Buttons, Buttons

There is a Show Owners button whose handler simply calls DisplayCarsAndOwners, which does a more complex selection of the data, joining the two tables and extracting the interesting data as a new ItemsSource for the DataGrid,

private void DisplayCarsAndOwners ()
{

var d = from item in context.GetAll<Car>()
join owner in context.GetAll<CarOwner>()
on item.OwnerFK equals owner.OwnerID
select new { owner.OwnerID, owner.Name, owner.Age, item.Model, item.YearOfModel };

dataGrid.ItemsSource = d;

}

Adding And Deleting

We’d like to be able to add a record to the database. Doing so is pretty easy. Rather than creating a UI to assemble the data, we hard code it, creating a new Car object and then calling Insert on the Context object, passing in the new car and then calling SaveChanges.

var car = new Car() { Model = "Mini Cooper S", 
RegistrationNumber = "ABC123",
yearOfModel = new DateTime( 2013, 7, 10 ),
OwnerFK = 3 };
context.Insert<Car>( car );
context.SaveChanges();

This adds a car to the database. We delete by finding all of that owner’s cars and deleting them,

private void Delete_Click_1( object sender, RoutedEventArgs e )
{
var target = context.GetAll<Car>().Where<Car>( car => car.OwnerFK == 3 );
foreach (Car car in target)
{
context.Delete<Car>( car );
}
context.SaveChanges();
DisplayCarsAndOwners();
}

Pretty straight forward. Once you get your first Database set up, it is almost trivial to work with the Local Data Service.



Win8_Download (2)

Download RadControls for WPF Download RadControls for Silverlight

About the author

Jesse Liberty

Jesse Liberty

Jesse Liberty is a Technical Evangelist for Telerik and has three decades of experience writing and delivering software projects. He is the author of 2 dozen books as well as courses on Pluralsight . Jesse has been a Distinguished Software Engineer for AT&T and a VP for Information Services for Citibank and a Software Architect for PBS. You can read more on his personal blog and his Telerik blog or follow him on twitter


jesseLiberty
About the Author

Jesse Liberty

 has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and has been a Distinguished Software Engineer for AT&T and a VP for Information Services for Citibank and a Software Architect for PBS. You can read more on his personal blog or follow him on twitter

Comments

Comments are disabled in preview mode.