Windows Phone 7 DB Connection Settings Helper


Background:

Lately I have been developing Windows Phone 7.5 application as part of the I Unlock Joy program. If you have followed development in Windows Phone 7 world, you will know that WP7.5 code named Mango now supports local database support for applications. So you can now develop application which have local DB support. The data base supported is SQL CE 3.5 and the data access technology is LINQ 2 SQL. Many examples on the internet will usually guide you to declare the connection string within the data context as a static string and declare it inline or a static string in the App class. I personally didn’t like that and went out to research on the connection string in WP7 itself. As any other data base connection string – SQL CE connection string also has a pre defined parameters to use for. So I thought of quickly putting up a helper class which will do all the magic under the hood but expose those different settings as properties. At the usage level you just have to instantiate this and set some properties and call a method to extract the connection string.


Local Database Connection Strings for Windows Phone:

Here is a MSDN article which describes the connection parameters thoroughly: http://msdn.microsoft.com/en-us/library/hh202861%28v=vs.92%29.aspx

Here is the same details in a tabular format:

image


Connection Settings Helper:

So now that we know the different parameters of local database connection string, all that was left for me was to wrap them in a class which I call it as WP7DBSettings and we are done. I have a interface defined by name IWP7DBSettings and WP7DBSettings class inherits from IWP7DBSettings. Here is the class diagram of the helper:

image

As you can see all I have done is to expose each parameter supported in the connection string as a property on the class. GetConnectionString() method is where all the magic is happening. It will build the connection string based on the data in the different properties.


Usage:

Below are some of the examples of how the WP7DBSettings file can be used:

  • Single Parameter Usage:

     
    image

    Output: "Data Source=’isostore:/mydb.sdf’;Max Buffer Size=384;Max Database Size=32;"

  • Reading from the Installation Folder:

    image

    Output: "Data Source=’appdata:/mydb.sdf’;File Mode=read only;Max Buffer Size=384;Max Database Size=32;"

  • Creating an Encrypted Database:

    image

    Output: "Data Source=’isostore:/mydb.sdf’;Password=’p@ssword’;Max Buffer Size=384;Max Database Size=32;"

  • Read-Only Connection:

    image

    Output: "Data Source=’appdata:/mydb.sdf’;File Mode=read only;Max Buffer Size=1024;Max Database Size=256;"

  • Creating a Database with Specific Culture:

    image

    Output: "Data Source=’isostore:/mydb.sdf’;Max Buffer Size=384;Max Database Size=32;Culture Identifier=32;Case Sensitive=true;"


Conclusion:

You might ask what is the difference between inline string usage and using helper? Well that was my first question to myself until I faced the following problem. I was using GalaSoft MvvmLight toolkit for the development. It comes with a  inbuilt DI or Dependency Injection containers called SimpleIoC. As I mentioned earlier my data context to DB was a LINQ2SQL data context and it generated the following constructors:

image

SimpleIoC at this point does not have a mechanism to inject a string as a dependency. So that’s when  the need arise to inject an object to one of the constructor. In your app launching or during your DI bootstrapping you can build this object and keep it in the container and inject it into the data context. The ways to use this class is upto your imagination and architecture. This certainly did help and wanted to share it with the community. If any of you out there also feel the same emotion here is the class itself available for download. You are free to download and make changes at your will and wish :).

Download File

Hope this post did help you in some way. Till next time as usual – Happy Coding. Code with Passion, Decode with Patience.