Facebook App ID is missing!

NoSQL on Windows

In this week’s episode of “Inside Windows Phone,” Matthijs Hoekstra and I discuss the use of NoSQL.

When building mobile apps for the enterprise, an offline data store is absolutely essential so that users can keep working in the absence of wireless connectivity.  In addition to SQLite and SQLCE, NoSQL is a great option to consider in order to achieve your goals with local data storage.

Below are some code snippets I created to get you started:

Define Table schema (Entity)
public sealed class Customer
{
    public int CustomerId { getset; }
    public string FirstName { getset; }
    public string LastName { getset; }
}

Define Table (Generic List to hold collection of Customer objects)
public static List<Customer> Customers { getset; }

 

Create Table
Customers = new List<Customer>();

 

Save Table
public static async Task SaveChanges<T>(T collection, String tableName)
{
    var file = awaitApplicationData.Current.  LocalFolder.CreateFileAsync(tableName, CreationCollisionOption.ReplaceExisting);
    var outStream = await file.OpenStreamForWriteAsync();
    var serializer = new DataContractJsonSerializer(typeof(T));
    serializer.WriteObject(outStream, collection);
    await outStream.FlushAsync();
    outStream.Dispose();
}
 
Calling the Save Table Method
await SaveChanges<List<Customer>>(Customers, “Customer”);
 
Load Table
public static async Task<T> LoadTable<T>(string tableName)
{
    StorageFile file = awaitApplicationData.Current.
LocalFolder.GetFileAsync(tableName);
    Stream stream = await file.OpenStreamForReadAsync();
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
    T table = (T)serializer.ReadObject(stream);
    stream.Dispose();
    return table;
}
 
Calling the Load Table Method
Customers = await LoadTable<List<Customer>>(“Customer”);
 
INSERT Customers
Customers.Add(new Customer
{
    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 in TableService.Customers
            select c;
CustomersList.ItemsSource = query.ToList();

 

Have fun!

Sharing my knowledge and helping others never stops, so connect with me on my blog at http://robtiffany.com , follow me on Twitter at https://twitter.com/RobTiffany and on LinkedIn at https://www.linkedin.com/in/robtiffany

Rob is a writer, teacher, speaker, world traveller and undersea explorer. He's also a thought leader in the areas of enterprise mobility and the Internet of Things.

1 comments On NoSQL on Windows

Leave a Reply