One of the major benefits to me of both Windows Phone 7 (WP7) and Monotouch is that I can write in .net. So although the visual-view side of coding for both is very different I should be able to use pretty much the same model & services right? Well sort of. Here are a couple of issues I’ve hit during a recent port;
I was quite surprised by this one. Monotouch, as does .net, has a number of database options but I guess a very common choice is mySQL. As such my persistence layer makes heavy use of SQL. However, currently there isn’t an implementation of mySQL available on WP7. I’m guessing it’s to do with only having the isolation storage stream available. I am aware of a some alternatives, Silverlight Database & Perst to name a couple, and I’ll certainly be taking a look at these in the future. However, for my little project I’m not storing a great amount of data so I’ve stuck with simple serialization techniques and a hefty use of Linq for now.
Reduced function set
One of the more interesting problems was hitting those functions that are “unavailable” in Silverlight but free to roam in Monotouch. My main example of this is XDocument.Load. My application makes use of a xml based web service to retrieve information for the user and as with most applications I need avoid using the UI thread to run this requests. The pattern I’d used for Monotouch was aimed a KISS since in my experience 2 of every 3 errors you get provide little or no clue as to what is actually causing the problem. So the idea is for the UI to make a single asynchronous call to the service and for that to make sequential calls to the underlying web service. Sure the overall time will be longer but the complexity is low;
The code I used to make the synchronous web service call was XDocument.Load. My original intention was to directly port the service code to WP7, this is where I hit this reduced function set problem in Silverlight. Silverlight does not allow XDocument.Load to go outside of the current XAP. What I’m supposed to do is make async calls to the web service via WebClient, well done Silverlight you’re forcing me to code properly. However, the code complexity has now risen because I now have to handle a number of events to satisfy the requirements;
So I now have the added complexity of gathering the async results from both service layers. Ok it’s not a bad thing to do but it does show that, rightly or wrongly, I have more flexibility to design (incorrectly?) a solution in Monotouch than I do in Silverlight. Although it also shows that I’m far more confident in getting sensible errors back in Silverlight, and for people that are only familiar with Silverlight I’ll let you draw your own conclusions about the quality of errors in Monotouch.
One irritating problem was Console.Write et al do not seem to produce any output to Visual Studio for WP7. Hopefully this is either something I’ve missed or a feature that will be there once out of Beta. I switched to System.Diagnostics.Debug instead.
This wasn’t intended as an in-depth study of porting between the two but simply to show that if you take a moment to spot where the differences are and investigate appropriate abstractions and patterns then you can save yourself a lot of time.