Search

Home > Inside Windows Phone (HD) - Channel 9 > IWP71 | NoSQL
Podcast: Inside Windows Phone (HD) - Channel 9
Episode:

IWP71 | NoSQL

Category: Technology
Duration: 00:20:48
Publish Date: 2014-01-16 01:08:47
Description:

This week we are meeting again with our big friend Rob Tiffany. This time we talk about NoSQL. It's an easy way to store your data offline on your phone. During this conversation Rob explains what NoSQL is, why you would use it and what the challenges are.

Below are the code samples how to perform the CRUD operations on your in memory classes.

Define Table schema (Entity)

publicsealedclassCustomer
{
    publicint CustomerId { get; set; }
    publicstring FirstName { get; set; }
    publicstring LastName { get; set; }
}

Define Table (Generic List to hold collection of Customer objects)

publicstaticList Customers { get; set; }

Create Table

Customers = newList();

Save Table

publicstaticasyncTask SaveChanges(T collection, String tableName)
{
    var file = awaitApplicationData.Current.LocalFolder.CreateFileAsync(tableName, CreationCollisionOption.ReplaceExisting);
    var outStream = await file.OpenStreamForWriteAsync();
    var serializer = newDataContractJsonSerializer(typeof(T));
    serializer.WriteObject(outStream, collection);
    await outStream.FlushAsync();
    outStream.Dispose();
}

Calling the Save Table Method

await SaveChanges>(Customers, "Customer");

Load Table

publicstaticasyncTask LoadTable(string tableName)
{
    StorageFile file = awaitApplicationData.Current.LocalFolder.GetFileAsync(tableName);
    Stream stream = await file.OpenStreamForReadAsync();
    DataContractJsonSerializer serializer = newDataContractJsonSerializer(typeof(T));
    T table = (T)serializer.ReadObject(stream);
    stream.Dispose();
    return table;
}

Calling the Load Table Method

Customers = await LoadTable>("Customer");

Insert Customers

Customers.Add(newCustomer
{
    CustomerId = 1,
    FirstName = "Andy",
    LastName = "Wigley"
});

Update Customers

foreach (var item in Customers.Where((c) => c.CustomerId == 2))
{
    item.FirstName = "Mike";
}

Delete Customers 

Customers.RemoveAll((c) => c.CustomerId == 2);

Select Customers

var query = from c inTableService.Customers
            select c;
 
CustomersList.ItemsSource = query.ToList();

Tweet to @robtiffany or @mahoekst

Total Play: 0