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.

Wednesday, May 25, 2011

Move aside and let the MANGO through!

If you're interested in Windows Phone 7 development and were offline, for whatever reason, today or have been sleeping under a rock, you may have missed that the Beta version of the next version of the Windows Phone 7 development tools (codename "Mango") were released today.


You can read more details at http://windowsteamblog.com/windows_phone/b/wpdev/archive/2011/05/24/developer-news-beta-mango-tools-available-today.aspx then get it from http://create.msdn.com/en-US/news/WPDT_7.1_Beta

Check out this video (sorry it's a bit wobbly-I hope you don't get sea sick watching it) to see what was said at the launch.

Tuesday, May 03, 2011

WP7 OS Version numbers

For my own reference and because I couldn't find anywhere else where this had been documented. Here are the different OS Version numbers for each of the WP7 releases/updates.

7.0.7004.0 - Original public release
7.0.7008.0 - "Pre-Nodo" update (contained changes to support the NoDo update)
7.0.7390.0 - NoDo (No-Donuts) update (included performance improvements and cut and paste)
7.0.7392.0 - Security update

Update: See http://www.microsoft.com/windowsphone/en-us/howto/wp7/basics/update-history.aspx

Tuesday, April 26, 2011

Minify the size of your XAP file with some help from Telerik

As every good Windows Phone 7 developer knows. It's generally a very good idea to make the size of your XAP file as small as possible. Among other things this can help towards faster app loading times.

For an app I've been working on recently (more off than on) I wanted to get the start up time as short as possible. One of the ways I've been doing this has been to remove unnecessary external libraries and to optimise my use of the ones that I do need.
Now, while I'm a big fan of NuGet sometimes I don't want everything in a library. In this instance I created a version of the library I was using specifically restricted to the functionality that the app needs. I did this by getting the source of the library I was using (well actually libraries as there were 2), added the source project to my solution, excluded everything I didn't need from the project and then linked my app to that project rather than the downloaded copy of the compiled library.

Before I started this process my app was 599Kb. After the above process I got it down to 298Kb but there were still a couple of large assemblies in my XAP file that were bugging me.
These were both from Telerik (part of their WP7 control suite) but as I was just using a small part of the functionality available in those libraries this bugged me. As source wasn't available, I couldn't use the above technique. So I tweeted about it.

A few hours later I had a very interesting response.

It turns out Telerik already have a solution to this problem. It's called the Telerik Minifier and it provides 2 option:
1. You tell it which objects you specifically want and it gives you a set of libraries which contain just those. or
2. You point it at your XAP file and it removes everything it can to make it smaller. (I'm guessing it does the process in option 1 but just manages the updating of the XAP file for you.)

I opted for option 2 and then a minute or so later my XAP file was down to 209Kb in size.

And yes, my app now opens super fast. So fast in fact that I'm not including a Splash Screen!

Update
After some questions about what's actually happening, have a look at the image below.


On the left is the contents of the XAP before minification. On the right is the contents of the XAP after minification. The Telerik controls have been replaced with new versions which only contain the necessary code for running the app.