Download Source Code **UPDATED for RTM 2 Nov 2010**
There has been some comment about the lack of a client side database for Windows Phone 7 Silverlight. Windows Phone 7 does have SQL Server Compact Edition installed in the OS image, but in the initial release, it is there to support the built-in apps only and has no public API to allow Silverlight developers to use it for data persistence.
However, there is a database available that WP7 Silverlight developers can use: it's called Perst .NET which is developed by McObject and the source code is pure C# and freely downloadable. As distributed, it works fine with the full .NET Framework, with the .NET Compact Framework and with desktop Silverlight, and they have a demo app of a CRM that runs in the desktop browser in Silverlight (read about it here). Using Perst for .NET, developers using Silverlight can now include true database management system features in their Silverlight applications, including adding persistence to this data by storing it in a container file in isolated storage.
I thought it would be fun to see if I could port the database and implement the CRM app on Windows Phone 7 and it wasn't too hard!
Here you can see the contacts list, and the details view when you select a contact from the list.
You can download the full source code from the link at the top of this post (developed with the Windows Phone 7 April Devtools refresh that you can download here) . Inside it you will find three projects:
- PerstWP7 - this is a Windows Phone 7 port of the database code. It doesn't have the replication code in it that the full .NET framework version supports, and it doesn't have a few methods in the database wrapper that require IQueryable, which is not supported on WP7.
- PerstDemo - This is the source code for the CRM app. This is a work in progress! At present, the app allows you to generate test data to fill the database, allows you to list, add, edit, delete and search Contacts. At present there is no UI to allow you to work with Leads and Activities, as the full Silverlight CRM demo app does, but I plan to add this over the coming months, perhaps using a panoramic view which would be nice!
- SilverlightToolkit.Controls.Input - Although according to blog posts such as this you are supposed to be able to use the Silverlight Toolkit libraries directly - at least for some of the controls in there - I couldn't get it to work with the current drop of the WP7 dev tools. I wanted an AutoSuggestTextbox to use in the search function, so I extracted the source for the toolkit version of this control and compiled it into this WP7 class library.
Using the App
When you start the app, you'll just see an empty heading of Contacts. Click the ellipses on the menu bar to reveal the menu for generating test data. Use the slider to select the total number of Contact, Lead and Activity objects to generate: for example, 5000 will give you a long list of a few hundred contacts.
As you play with the app, you'll see that you can view and edit details of Contacts in the list, you can delete contacts and you can add new contacts by tapping the '+' icon at the foot of the main screen.
Full Text Search
'Well, is this really a database?' you might be asking. Well - how about this: it's got a Full Text Search facility! If you tap on the Search icon on the menu bar, you get a Search box appear, which is the AutoSuggestTextBox. As you enter characters into the search box, it displays a list of contacts that match as you type in a drop down list. You can select a contact from the dropdown, or just display a subset of contacts that include the search string in any of the Full Text Search index fields, which are LastName, FirstName and CompanyName.
Using Perst for Commercial Use.
One point to make about this: Perst is free to use for non-commercial use and is released under the GPL license. Perst is cross-platform as there is a Java version as well as the .NET one, and Perst is already used in J2ME, on Android and on .NET platforms. If you want to use it in a commercial app, then a license fee applies which should only amount to a few pennies per copy. ** UPDATE ** See my other post here for details of a new licensing deal for Perst.
Over the next few days I will follow up with a few posts showing how you write code to work with a Perst database. In the meantime, please download the sample code and take a look at how it works.
In this sample, we are using Perst's database wrapper which gives relational database-like behaviour so you create tables and keys and store and retrieve objects from tables in a manner that will be familiar to anyone who has worked with relational databases before. However, Perst is actually an object database, and if you don't use the database wrapper, you simply work with .NET classes in exactly the same way an any other .NET object, but it just so happens that these objects have the ability to be persisted. That's really interesting to me as I hate writing all that unnecessary object relational mapping (ORM) code to convert objects to and from their database format. However, that's a topic for another day.
Over time, I plan on extending this sample to add the UI for working on Leads and Activities as well - it's a learning exercise for me to really learn WP7 development in depth, and hopefully will be of interest to you too!
Perst - a database for Windows Phone 7 Silverlight - Part 2
Jun 07 2010, 06:10 AM