Over a million developers have joined DZone.

Simplifying use of SQL CE in Mango (1 of n)

· Mobile Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

One of the features that the "Mango" tools introduce for Windows Phone development is a built in SQL Compact Edition (SQL CE) database. While learning about how to use it I was struck by how much better the experience could be.

As such, here is the first in what I hope will be a series of pointers and tips for making working with a database easier.

If you want to learn more about using SQL CE with Windows Phone codename "Mango" then I also recommend you check out the following on MSDN:

How to: Create a Basic Local Database Application for Windows Phone


Hands-On-Lab: Using local database in To-do application

When working with a database it's good practice to persist any changes to the datacache as quickly as possible. This means you'll probably end up doing something like this:
or this:
quite a lot.

The only reason you may not want to do the above (Calling `SubmitChanges` after every call to `XxxxxOnSubmit()`) is if you were performing a number of insertions or deletions in a loop. In this case it may be wise (more performant) to call `SubmitChanges()` at the end.

When looking at even a short amount of code that does insertions or deletions it strikes me that there is a lot of duplication and that if those functions are always called together then it may make sense to bundle them up into a single function.

As it may not be immediately obvious how to do this (I had to explore a couple of options before I ended up with this), I present a simple class with some extension methods which does just that:
public static class TableExtensions
  public static void InsertAndSubmitChanges<t>(this Table<t> table, T entity) where T : class
  public static void DeleteAndSubmitChanges<t>(this Table<t> table, T entity) where T : class
By using the above class, it means that where we were previously having to write 2 lines of code we can now just write 1 and still get the same functionality!
I hope this helps you write less code. (In a good way of course.)
Please let me know if you find this helpful.

The Mobile Zone is brought to you in partnership with Strongloop and IBM.  Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud.


Published at DZone with permission of Matt Lacey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}