Subscribe to the RSS feed then follow me on twitter at @mrlacey

Thursday, May 26, 2011

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

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
and
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:
  db.MyItems.InsertOnSubmit(myNewItemInstance);
  db.SubmitChanges();
or this:
  db.MyItems.DeleteOnSubmit(anItemInstance);
  db.SubmitChanges();
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(this Table table, T entity) where T : class
  {
    table.InsertOnSubmit(entity);
    table.Context.SubmitChanges();
  }

  public static void DeleteAndSubmitChanges(this Table table, T entity) where T : class
  {
    table.DeleteOnSubmit(entity);
    table.Context.SubmitChanges();
  }
}  

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!

  db.MyItems.InsertAndSubmitChanges(myNewItemInstance);

  db.MyItems.DeleteAndSubmitChanges(anItemInstance);  

I hope this helps you write less code. (In a good way of course.)
Please let me know if you find this helpful.

If you've built Windows Phone or Windows Store apps you can cross-promote them with AdDuplex to get more users.

1 comment:

  1. We will have a very peek in the excess. Yet another popular breitling replica sale involving donning this specific enjoy can be setting the idea involving colourful piled bracelet that will select your current clothing. Ornamented Designer watches: These kind of enjoy companies are generally popular because of their good quality quality, artistic lure along with reputation the idea has as well as the idea. For you to omega replica uk way up just about any clothing and convey a sprint involving coloring for it; ornamented designer watches using rhinestones, legitimate precious gems along with material gems can be a excellent part for virtually any nighttime bash. These are deluxe ample to switch a new precious stone as well as platinum encrusted bracelet. For you to jazz way up your look these are genuine components of classiness so that it is absolutely a new manner affirmation. These kind of lovely along with lovely fake rado are generally ideal for any circumstance. rolex replica sale Designer watches: Material designer watches are actually discovered all over the place and the wonderful similar to his or her simplicity of laid-back along with elegant employ explained straight into one particular. The opposite root of huge acceptance of an material enjoy can be the number of choices there is. Watches made out of yellow gold, bright platinum, us platinum, silver precious metal, went up by platinum, water piping, titanium are generally many of the long-lasting material selections you'll be able to select. In case you would like a watch that might very last a long time; material rolex replica sale can be a excellent selection you must rely upon.

    ReplyDelete